Would this work on all platforms? i know windows does \r\n, and remember hearing mac does \r while linux did \n. I ran this code on windows so it seems fine, but do any of you know if its cross platform?
while 1: line = f.readline() if line == '': break line = line[:-1] print '\'' + line + '\''
First of all, there is universal newline support
Second: just use
line.strip(). Useline.rstrip('\r\n'), if you want to preserve any whitespace at the beginning or end of the line.Oh, and
or at least
might look a bit nicer.
You can iterate over the lines in a file like this (this will not break on empty lines in the middle of the file like your code):
If your input file is short enough, you can use the fact that
str.splitlinesthrows away the line endings by default: