[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Line terminators in Python?

On 9/29/17, Rob Gaddi <rgaddi at highlandtechnology.invalid> wrote:
> On 09/29/2017 10:54 AM, Stefan Ram 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?
> As everyone else has said; for general purpose (any of your cases) use
> you should always just use '\n' and it automagically works.  The only
> time I've ever needed to explicitly worry about '\r' is communicating
> over sockets or serial ports to devices.  And in those cases you need to
> stuff them with bytes rather than str anyhow, so you're already down in
> the gory bits.
> --
> Rob Gaddi, Highland Technology -- www.highlandtechnology.com

There is also https://docs.python.org/3/library/csv.html#csv.Dialect.lineterminator
which (I think) come from RFC4180 (see
http://www.rfc-archive.org/getrfc.php?rfc=4180 search CRLF)

http://www.rfc-archive.org/getrfc.php?rfc=2046 could be also
interesting in this case.