Monday, September 22, 2008

Do you know Universal Newline Support in Python?

Do you know Universal Newline Support in Python? To be honest, I didn't know that. The PEP 278 describes in detail:

In a Python with universal newline support open() the mode parameter can also be "U", meaning "open for input as a text file with universal newline interpretation". Mode "rU" is also allowed, for symmetry with "rb". Mode "U" cannot be combined with other mode flags such as "+". Any line ending in the input file will be seen as a '\n' in Python, so little other code has to change to handle universal newlines.

Conversion of newlines happens in all calls that read data: read(), readline(), readlines(), etc.

Here is an example:

% printf 'Hello, World!\r\n' > /tmp/hello.txt
% cat /tmp/hello.txt 
Hello, World!
% python 
Python 2.5.1 (r251:54863, Apr 15 2008, 22:57:26) 
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> open('/tmp/hello.txt').read()
'Hello, World!\r\n'
>>> open('/tmp/hello.txt', 'U').read()
'Hello, World!\n'

If Python was built with the --with-universal-newlines option to configure (the default), File objects have newlines attribute, so testing whether the current Python has universal newline support can be done with:

if hasattr(sys.__stdout__, "newlines"):
    print "universal line endings"

0 comments: