File Parsing Question
Peter Otten
__peter__ at web.de
Thu Sep 13 02:49:22 EDT 2007
Dennis Lee Bieber wrote:
> for line in inp:
>
> will read one line at a time (I'm fairly sure the iterator doesn't
> attempt to buffer multiple lines behind the scenes)
You are wrong:
>>> open("tmp.txt", "w").writelines("%s\n" % (9*c) for c in "ABCDE")
>>> instream = open("tmp.txt")
>>> for line in instream:
... print instream.tell(), line.strip()
...
50 AAAAAAAAA
50 BBBBBBBBB
50 CCCCCCCCC
50 DDDDDDDDD
50 EEEEEEEEE
>>>
Here's the workaround:
>>> instream = open("tmp.txt")
>>> for line in iter(instream.readline, ""):
... print instream.tell(), line.strip()
...
10 AAAAAAAAA
20 BBBBBBBBB
30 CCCCCCCCC
40 DDDDDDDDD
50 EEEEEEEEE
>>>
Peter
More information about the Python-list
mailing list