我使用的模块是商业软件API的一部分。好消息是有一个python模块 - 坏消息是它非常不错。更多pythonic方式来迭代
遍历行时,follwoing语法用于:
cursor = gp.getcursor(table)
row = cursor.next()
while row:
#do something with row
row = cursor.next()
什么是处理这种情况的最Python的方式?我也考虑创造一流的功能/发电机和它包装调用一个循环:
def cursor_iterator(cursor):
row = cursor.next()
while row:
yield row
row = cursor.next()
[...]
cursor = gp.getcursor(table)
for row in cursor_iterator(cursor):
# do something with row
这是一个进步,但感觉有点笨拙。是否有更多pythonic方法?我应该围绕table
类型创建包装类吗?
Mmmh'cursor.next()'看起来像你*可能*能够为光标中的行做': – 2010-06-05 10:56:35
@Felix,nope。 'next'不会引发['StopIteration'](http://docs.python.org/library/exceptions.html#exceptions.StopIteration),所以在真正的数据之后,这只会使用'row'是'没有'。 – 2010-06-05 11:00:40
@Felix King:差不多,但因为一个迭代器通过提高StopIteration信号来终止信号,所以'cursor in cursor:'中的行将遍历行,然后给出无限的'None'流。 – 2010-06-05 11:00:51