简体   繁体   中英

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 figure 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM