Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #89897
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Subject | Re: when does newlines get set in universal newlines mode? |
| Date | 2015-05-04 14:01 +0200 |
| Organization | None |
| References | <3c45772b-77e0-4c17-8b3d-aa246c4b511c@googlegroups.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.81.1430740879.12865.python-list@python.org> (permalink) |
arekfu@gmail.com wrote:
> Hi all,
>
> I have a text file with Windows-style line terminators (\r\n) which I open
> in universal newlines mode (Python 2.7). I would expect the newlines
> attribute to be set after the first call to the readline() method, but
> apparently this is not the case:
>
>>>> f=open('test_crlf', 'rU')
>>>> f.newlines
>>>> f.readline()
> 'foo\n'
>>>> f.newlines
>>>> f.readline()
> 'bar\n'
>>>> f.newlines
> '\r\n'
> On the other hand, the newlines attribute gets set after the first call to
> readline() on a file with Unix-style line endings.
>
> Is this a bug or a feature?
According to
https://docs.python.org/2.7/library/functions.html#open
"""
If Python is built without universal newlines support a mode with 'U' is the
same as normal text mode. Note that file objects so opened also have an
attribute called newlines which has a value of None (if no newlines have yet
been seen), '\n', '\r', '\r\n', or a tuple containing all the newline types
seen.
"""
I tried:
>>> with open("tmp.txt", "wb") as f: f.write("alpha\r\nbeta\rgamma\n")
...
>>> f = open("tmp.txt", "rU")
>>> f.newlines
>>> f.readline()
'alpha\n'
>>> f.newlines
# expected: '\r\n'
>>> f.readline()
'beta\n'
>>> f.newlines
'\r\n' # expected: ('\r', '\r\n')
>>> f.readline()
'gamma\n'
>>> f.newlines
('\r', '\n', '\r\n')
I believe this is a bug.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
when does newlines get set in universal newlines mode? arekfu@gmail.com - 2015-05-04 02:50 -0700
Re: when does newlines get set in universal newlines mode? Peter Otten <__peter__@web.de> - 2015-05-04 14:01 +0200
Re: when does newlines get set in universal newlines mode? Chris Angelico <rosuav@gmail.com> - 2015-05-04 22:13 +1000
Re: when does newlines get set in universal newlines mode? Davide Mancusi <arekfu@gmail.com> - 2015-05-04 06:35 -0700
Re: when does newlines get set in universal newlines mode? Terry Reedy <tjreedy@udel.edu> - 2015-05-04 13:38 -0400
Re: when does newlines get set in universal newlines mode? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-05-05 18:31 +1000
Re: when does newlines get set in universal newlines mode? Chris Angelico <rosuav@gmail.com> - 2015-05-05 18:41 +1000
Re: when does newlines get set in universal newlines mode? Davide Mancusi <arekfu@gmail.com> - 2015-05-05 02:23 -0700
Re: when does newlines get set in universal newlines mode? Chris Angelico <rosuav@gmail.com> - 2015-05-05 19:28 +1000
Re: when does newlines get set in universal newlines mode? Davide Mancusi <arekfu@gmail.com> - 2015-05-05 03:58 -0700
Re: when does newlines get set in universal newlines mode? Peter Otten <__peter__@web.de> - 2015-05-04 17:17 +0200
Re: when does newlines get set in universal newlines mode? Chris Angelico <rosuav@gmail.com> - 2015-05-05 01:26 +1000
Re: when does newlines get set in universal newlines mode? Ian Kelly <ian.g.kelly@gmail.com> - 2015-05-04 09:33 -0600
csiph-web