Populating a list

Peter Hansen peter at engcorp.com
Mon Dec 10 01:44:24 EST 2001


Bruce Eckel wrote:
> 
> I would probably do it like this:
> lines = [l.rstrip() for l in open('filename')]
> In Python 2.2 you don't have to say "readlines", as open() produces
> an iterator.
> Using rstrip(), I believe, handles more general cases (some oses
> have two characters to end a line, rather than just one).

Although then you run the risk of removing useful 
information, if trailing whitespace (other than the
line terminating sequence) is supposed to be preserved.

As usual, it depends on the original requirements...

-- 
----------------------
Peter Hansen, P.Eng.
peter at engcorp.com



More information about the Python-list mailing list