stripping

Tim Williams tdw at tdw.net
Tue May 2 13:12:48 CEST 2006


On 02/05/06, Tim Williams <tdw at tdw.net> wrote:
>
>
> On 1 May 2006 23:20:56 -0700, micklee74 at hotmail.com <micklee74 at hotmail.com
> > wrote:
> >
> > hi
> > i have a file test.dat eg
> >
>
>
> f = open("test.dat")
> while 1:
>         line = f.readline().rstrip("\n")
>         if line:
>              print line
>
> is simpler and easier to read.
>

Except for my obvious mistake, tThe while never terminates:

>>> for line in f:
...     line = line.rstrip("\n")
...     if line:
...         print line
...
abcdefgh
ijklmn
opqrs
tuvwxyz

or even

>>> for line in open("test.dat"):
...     line = line.rstrip("\n")
...     if line:
...         print line
...
abcdefgh
ijklmn
opqrs
tuvwxyz

:)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20060502/69f05446/attachment.html>


More information about the Python-list mailing list