Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #32684

Re: enabling universal newline

From Peter Otten <__peter__@web.de>
Subject Re: enabling universal newline
Date 2012-11-03 09:26 +0100
Organization None
References <alpine.DEB.2.00.1211022316290.2869@pebbe> <509452ae$0$29967$c3e8da3$5496439d@news.astraweb.com>
Newsgroups comp.lang.python
Message-ID <mailman.3232.1351931198.27098.python-list@python.org> (permalink)

Show all headers | View raw


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.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

enabling universal newline Peter Kleiweg <pkleiweg@xs4all.nl> - 2012-11-02 23:22 +0100
  Re: enabling universal newline Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-11-02 23:09 +0000
    Re: enabling universal newline Peter Otten <__peter__@web.de> - 2012-11-03 09:26 +0100
    Re: enabling universal newline Peter Kleiweg <pkleiweg@xs4all.nl> - 2012-11-03 09:30 +0100

csiph-web