Splitting text into lines

Peter Otten __peter__ at web.de
Tue Dec 13 12:02:02 EST 2016

George Trojan - NOAA Federal wrote:

> I have files containing ASCII text with line s separated by '\r\r\n'.

> but it looks cumbersome. I Python2.x I stripped '\r' before passing the
> string to split():
>>>> open('FTAK31_PANC_131140.1481629265635').read().replace('\r', '')
> 'FTAK31 PANC 131140\nTAFABE\nTAF\nPABE 131140Z 1312/1412 07010KT P6SM
> SCT035 OVC060\n     FM132100 10012G20KT P6SM BKN100 WS015/18035KT\n
> FM141000 09015G25KT P6SM BKN050 WS015/18040KT=\n'
> but Python 3.x replaces '\r\r\n' by '\n\n' on read().

Tell Python to keep the newline chars as seen with

open(filename, newline="")

For example:

>>> open("odd-newlines.txt", "rb").read()

>>> open("odd-newlines.txt", "r", newline="").read().replace("\r", 
['alpha', 'beta', 'gamma', 'delta']

> Ideally I'd like to have code that handles both '\r\r\n' and '\n' as the
> split character.

More information about the Python-list mailing list