python docs的iter函数示例:
with open("mydata.txt") as fp:
for line in iter(fp.readline):
print line
给我这个:
Traceback (most recent call last):
File "
怎么可能? (Python 2.6.4)
最佳答案
这是因为我很蠢,读不懂:
Without a second argument,o must be a collection object which supports the iteration protocol (the iter() method),or it must support the sequence protocol (the getitem() method with integer arguments starting at 0). If it does not support either of those protocols,TypeError is raised.
解决方案是提供一个空字符串sentinel.
with open("mydata.txt") as fp:
for line in iter(fp.readline,''):
print line
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。