File Parsing Question

Shankarjee Krishnamoorthi shankarjee at gmail.com
Thu Sep 13 09:28:27 EDT 2007


Great. That worked for me. I had some of my routines implemented in
Perl earlier. Now that I started using Python I am trying to do all my
automation scripts with Python. Thanks a ton

Jee

On 9/13/07, Peter Otten <__peter__ at web.de> wrote:
> 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
> --
> http://mail.python.org/mailman/listinfo/python-list
>



More information about the Python-list mailing list