Python: Specify end-of-line format for reading files
I'm writing a Python script which processes a text file. I expect to process files generated from different people, working under different operating systems. Is there a nice way to figur开发者_如何转开发e out which OS created the text file, and specify the end-of-line convention to make parsing line-by-line trivial?
Use universal newline mode when opening the file.
with open('input.txt', 'rU') as fp:
for line in fp:
print line
splitlines()
handles various line terminators:
>>> 'foo\nbar'.splitlines()
['foo', 'bar']
>>> 'foo\rbar'.splitlines()
['foo', 'bar']
>>> 'foo\r\nbar'.splitlines()
['foo', 'bar']
If you do not care about ending white space then:
for line in [l.rstrip() for l in open('test.py').read().split('\n')]:
print line
'\n' will take care of Linux / Mac and rstrip will eat up any '\r' from Windows.
You want to use file.readlines()
, which returns a list containing the lines in the file.
lines = open('info.txt').readlines()
for line in lines:
print line
See the documentation on Python file objects.
精彩评论