newline - How to split only on carriage returns with readlines in python? -
i have text file contains both \n
, \r\n
end-of-line markers. want split on \r\n
, can't figure out way python's readlines method. there simple workaround this?
as @eskaev mentions, you'll want avoid reading complete file memory if not necessary.
io.open()
allows specify newline
keyword argument, can still iterate on lines , have them split only @ specified newlines:
import io line in io.open('in.txt', newline='\r\n'): print repr(line)
output:
u'this\nis\nsome\r\n' u'text\nwith\nnewlines.'
Comments
Post a Comment