[Python-3000] [Python-Dev] Universal newlines support in Python 3.0
Guido van Rossum
guido at python.org
Tue Aug 14 18:52:47 CEST 2007
On 8/14/07, Barry Warsaw <barry at python.org> wrote:
> It would have been perfect, I think, if I could have opened the file
> in text mode so that read() gave me strings, with universal newlines
> and preservation of line endings (i.e. no translation to \n).
You can do that already, by passing newline="\n" to the open()
function when using text mode. Try this script for a demo:
f = open("@", "wb")
f.write("bare nl\n"
"crlf\r\n"
"bare nl\n"
"crlf\r\n")
f.close()
f = open("@", "r") # default, universal newlines mode
print(f.readlines())
f.close()
f = open("@", "r", newline="\n") # recognize only \n as newline
print(f.readlines())
f.close()
This outputs:
['bare nl\n', 'crlf\n', 'bare nl\n', 'crlf\n']
['bare nl\n', 'crlf\r\n', 'bare nl\n', 'crlf\r\n']
Now, this doesn't support bare \r as line terminator, but I doubt you
care much about that (unless you want to port the email package to Mac
OS 9 :-).
--
--Guido van Rossum (home page: http://www.python.org/~guido/)
More information about the Python-3000
mailing list