[Python-Dev] [python] Re: New lines, carriage returns, and Windows

Greg Ewing greg.ewing at canterbury.ac.nz
Mon Oct 1 02:17:07 CEST 2007

Nick Maclaren wrote:
> I don't know PRECISELY what you mean by "universal newlines mode"

I mean precisely what Python means by the term: any of
"\r", "\n" or "\r\n" represent a newline, and no distinction
is made between them.

You only need to use that if you don't know what convention
is being used by the file you're reading. And if you don't
know that, you've already lost information about what the
contents of the file means, and there's nothing that any
I/O system can do to get it back.

> Model 1:  certain characters can be used only in combination.
 > ...

That's all fine if you know the file adheres to those
conventions. Just open it in binary mode and go for it.

The I/O systems of C and/or Python are designed for
environments where the files *don't* adhere to conventions
as helpful as that. They're making the best of what they're

> Note that the above is what the program sees - what is written
> to the outside world and how input is read is another matter.
> But I can assure you, from my own and many other people's experience,
> that neither of the above models cause the confusion being shown by
> the postings in this thread.

There's no confusion about how newlines are represented
*inside* a Python program. The convention is quite clear -
a newline is "\n" and only "\n". Confusion only arises when
people try to process strings internally that don't adhere
to that convention.

Greg Ewing, Computer Science Dept, +--------------------------------------+
University of Canterbury,	   | Carpe post meridiem!          	  |
Christchurch, New Zealand	   | (I'm not a morning person.)          |
greg.ewing at canterbury.ac.nz	   +--------------------------------------+

More information about the Python-Dev mailing list