Skip to content Skip to sidebar Skip to footer

Is Universal Newlines Mode Supposed To Be Default Behaviour For Open() In Python 2.7?

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 do

Solution 1:

You're observing this because \r\n is the line terminator on Windows, so the t mode converts it to \n. On Unix (MacOS here), t doesn't affect \r\n and there's no conversion. The difference between t and U is that U converts \r\n and \r to \n on every platform, while t is platform dependent and only converts LT for the given platform.

Replace your test string to "One\r\nTwo\nThree\rFour" to see the effect of U.

Solution 2:

This documentation explains it.

Basically when you open as a text file (without 'b'), the end-of-line characters in text files are automatically altered slightly when data is read or written. Use binary mode if you don't want this.

Post a Comment for "Is Universal Newlines Mode Supposed To Be Default Behaviour For Open() In Python 2.7?"