enabling universal newline

Peter Otten __peter__ at web.de
Sat Nov 3 09:26:39 CET 2012

Steven D'Aprano wrote:

> On Fri, 02 Nov 2012 23:22:53 +0100, Peter Kleiweg wrote:
>> In Python 3.1 and 3.2
>> At start-up, the value of sys.stdin.newlines is None, which means,
>> universal newline should be enabled. But it isn't.
> What makes you think it is not enabled?

$ python3 -c 'open("tmp.txt", "wb").write(b"a\nb\r\nc\rd")'

This is the output with universal newlines:

$ python3 -c 'print(open("tmp.txt").readlines())'
['a\n', 'b\n', 'c\n', 'd']

But this is what you get from stdin:

$ cat tmp.txt | python3 -c 'import sys; print(sys.stdin.readlines())'
['a\n', 'b\r\n', 'c\rd']

With Peter Kleiweg's fix:

$ cat tmp.txt | python3 -c 'import sys, io; print(io.TextIOWrapper(sys.stdin.detach(), newline=None).readlines())'
['a\n', 'b\n', 'c\n', 'd']

I think it's reasonable to make the latter the default.

More information about the Python-list mailing list