Line terminators in Python?

Chris Angelico rosuav at gmail.com
Fri Sep 29 14:24:25 EDT 2017


On Sat, Sep 30, 2017 at 3:54 AM, Stefan Ram <ram at zedat.fu-berlin.de> wrote:
>   In some languages, printing »'\n'«, the Unicode code point 10,
>   will have the effect of printing a line terminator, which might
>   mean that the output device actually receives »\r\n«.
>
>   The line terminator ostensibly depends on the operating
>   system, but actually it depends on the output system. E.g.,
>   under one operating system the console might accept another
>   set of line separators than an editor. (Under Windows,
>   »wordpad« accepts »\n«, while »notepad« requires »\r\n«.)
>
>   What is the recommended way to terminate a line written with
>   Python? Is it »\n« or something else? For example, in Java,
>   in some cases, one should terminate the line with the value
>   of »java.lang.System.lineSeparator()« which might or might
>   not be equal to the value of »"\n"«.
>
>   Does it possibly depend on the entity being written to, which
>   might be
>
>       - the Python console,
>       - the IDLE console,
>       - the operating system console or
>       - a text file?
>

Always use "\n". In the event that you actually need "\r\n", transform
that on output. In effect, you can treat "Windows-style newlines" vs
"POSIX-style newlines" as a form of text encoding, to be dealt with at
boundary locations only; when you read a file, you clean up the
newlines, and when you write, you *might* transform them. Notepad
sucks, so don't do things just for its sake; but certain network
protocols stipulate carriage return/line feed as their end-of-line.
HTTP, for instance, is clearly specified as such in RFC 2616 [1] -
section 19.3 recommends that "\n" be accepted for the sake of
interoperability, but the official line ending is CR LF. So if you're
writing to a socket, you might do a two-step process of transforming
newlines and also encoding UTF-8, but inside the application, keep
everything as Unicode text with "\n" between lines.

ChrisA

[1] https://www.ietf.org/rfc/rfc2616.txt



More information about the Python-list mailing list