I’m running Python 2.7.2 (64 Bit) on Windows 7. I’m a little confused about ‘universal newline mode’ documented here: http://docs.python.org/library/functions.html#open
From the documentation it seems ‘universal newline mode’ should not be in effect unless a ‘U’ is specified in the mode parameter of open(). However I see that as the default behavior! So is the documentation indeed misleading or am I missing something?
f = open("c:/Temp/test.txt", "wb")
f.write("One\r\nTwo\r\nThree\r\nFour"); f.close()
f = open("c:/Temp/test.txt", "rb")
f.read(); f.close()
'One\r\nTwo\r\nThree\r\nFour'
f = open("c:/Temp/test.txt", "r")
f.read(); f.close()
'One\nTwo\nThree\nFour'
f = open("c:/Temp/test.txt", "rt")
f.read(); f.close()
'One\nTwo\nThree\nFour'
f = open("c:/Temp/test.txt", "rU")
f.read(); f.close()
'One\nTwo\nThree\nFour'
Seems like “r”, “rt”, “rU” all have the same behavior?
You’re observing this because
\r\nis the line terminator on Windows, so thetmode converts it to\n. On Unix (MacOS here),tdoesn’t affect\r\nand there’s no conversion. The difference betweentandUis thatUconverts\r\nand\rto\non every platform, whiletis platform dependent and only converts LT for the given platform.Replace your test string to
"One\r\nTwo\nThree\rFour"to see the effect ofU.