gpt4 book ai didi

python - 如何多次迭代 ResultProxy?

转载 作者:行者123 更新时间:2023-11-28 21:45:06 28 4
gpt4 key购买 nike

假设我执行以下查询:

results = db.engine.execute(sql_query)

它按预期返回行的位置:

>>> for record in results:
... print(record['path'])
...
Top.Collections.Pictures.Astronomy.Stars
Top.Collections.Pictures.Astronomy.Galaxies
Top.Collections.Pictures.Astronomy.Astronauts

当我尝试第二次或第三次迭代它时,对象为空:

>>> for record in results:
... print(record['path'])
...
>>>

如何保存并重复使用 ResultProxy 多次迭代它?

最佳答案

您可能有一个 iterator 对象被返回,所以一旦迭代完它,您就不能再遍历它了。假设您的结果不是非常大,您可以:

results = db.engine.execute(sql_query) 
results = list(results)

这会将迭代器对象变成一个列表,您可以对其进行多次迭代。

关于python - 如何多次迭代 ResultProxy?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39880685/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com