'U'
mode:
Python 2:
I guess it may depend on what you're reading from, but the built-in open() function takes a 'mode' parameter, and if you pass 'U' for the mode, Python 2 will take care of the newlines in a cross-platform way transparently. It requires that Python be built with universal newline support, but test it out!
https://docs.python.org/2/library/functions.html#open
Python 3:
In Python 3, the 'U'
mode is the default behaviour, as the docs explain:
There is an additional mode character permitted, 'U'
, which no longer has any effect, and is considered deprecated. It previously enabled universal newlines in text mode, which became the default behaviour in Python 3.0. Refer to the documentation of the newline parameter for further details.
https://docs.python.org/3/library/functions.html#open
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…