BIG problem if readline strips newlines

Pete Shinners shredwheat at mediaone.net
Tue Jun 12 12:44:07 EDT 2001


i've seen frequent mention of an option for readline()
to strip out newlines as each string is read. this sounds
like a great idea, but i realized its gonna be very
difficult to loop through a text file. the standard
python idioms will no longer work...

for line in file.readlines(chomp=1):
    pass

while 1:
    line = file.readline(chomp=1)
    if not line: break
    pass


neither of these constructs will work because the
loop will break once there is a blank line with only
a linefeed ("\n"). i can't quite imagine what a basic
loop through a file would look like if the blank lines
had no linefeeds?

while 1:
    line = file.readline(chomp=1)
    if line == None: break
    pass

i suppose thats not too bad, but would there be a
better way all around?





More information about the Python-list mailing list