开发者

Python: Specify end-of-line format for reading files

开发者 https://www.devze.com 2023-01-28 15:41 出处:网络
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开发者_如何转

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消