Preserve end-of-line style when working with files in python

廉价感情. 提交于 2019-12-05 00:41:18

Use python's universal newline support:

f = open('randomthing.py', 'rU')
fdata = f.read()
newlines = f.newlines
print repr(newlines)

newlines contains the file's delimiter or a tuple of delimiters if the file uses a mix of delimiters.

Steven Brown

To preserve original line endings, use newline='' to read or write line endings untranslated.

with open('test.txt','r',newline='') as rf:
    content = rf.read()
content = content.replace('old text','new text')
with open('testnew.txt','w',newline='') as wf:
    wf.write(content)

Note that if the text manipulation itself deals with line endings, additional or alternative logic may be needed to detect and match original line endings.

The 'U' mode also works, but is deprecated.

Python Documentation: open

newline controls how universal newlines mode works (it only applies to text mode). It can be None, '', '\n', '\r', and '\r\n'. It works as follows:

• When reading input from the stream, if newline is None, universal newlines mode is enabled. Lines in the input can end in '\n', '\r', or '\r\n', and these are translated into '\n' before being returned to the caller. If it is '', universal newlines mode is enabled, but line endings are returned to the caller untranslated. If it has any of the other legal values, input lines are only terminated by the given string, and the line ending is returned to the caller untranslated.

• When writing output to the stream, if newline is None, any '\n' characters written are translated to the system default line separator, os.linesep. If newline is '' or '\n', no translation takes place. If newline is any of the other legal values, any '\n' characters written are translated to the given string.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!