Equivalent of Perl chomp?

Jason Orendorff jason at jorendorff.com
Thu Jan 31 09:11:27 EST 2002


Paul Watson wrote:
> Thank you for all of the replies.  Given the replies, I am wondering if
> Python treats "\n" as a platform independent newline.  I know it does when
> using print.  However, the re.sub() example below makes me think
> that \n is only a literal LF and does not resolve to CRLF on the
> DOS/Windows platform.
> Is this just a function of it being used in re?  Yes, clearly, "\n"
> generates CRLF when used in a print statement.

'\n' is not a CRLF in memory, on any platform (that I know).

It generates CRLF whenever it is written to a file opened for
text output (mode 'w').  Likewise, reading a file in text mode,
CRLF ('\r\n') gets converted to '\n'.  The C standard library
for Windows does this.

So yes, '\n' is treated as a platform-independent newline, and
it is not Python that handles this, but C.

Not sure about Java.  Perhaps Jython has to emulate this behavior.

## Jason Orendorff    http://www.jorendorff.com/





More information about the Python-list mailing list