osdir.com


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

Line terminators in Python?


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
Email address domain is currently out of order.  See above to fix.