I have a text file that contains both \n and \r\n end-of-line markers. I want to split only on \r\n, but can't figure out a way to do this with python's readlines method. Is there a simple workaround for this?
解决方案
As @eskaev mentions, you'll usually want to avoid reading the complete file into memory if not necessary.
io.open() allows you to specify a newline keyword argument, so you can still iterate over lines and have them split only at the specified newlines:
import io
for line in io.open('in.txt', newline='\r\n'):
print repr(line)
Output:
u'this\nis\nsome\r\n'
u'text\nwith\nnewlines.'