.split() Qeustion

Chris Angelico rosuav at gmail.com
Thu Aug 15 05:54:10 EDT 2013


On Thu, Aug 15, 2013 at 10:46 AM,  <wxjmfauth at gmail.com> wrote:
> A technical ascpect of triple quoted strings is
> that the "end of lines" are not respected.
>
>>>> import zzz
>>>> zzz.__doc__
> 'abc\ndef\n'
>>>> with open('zzz.py', 'rb') as fo:
> ...     r = fo.read()
> ...
>>>> r
> b'"""abc\r\ndef\r\n"""\r\n'
>
> Now, one can argue...

Actually, they are respected. Triple-quoted strings are parsed after
the file is read in, and newline handling is dealt with at an earlier
level, same as encodings are. You wouldn't expect that string to
retain information about the source file's encoding, and nor do you
expect it to retain the source file's newline style. A newline is
represented in the file on disk as \r\n or \n (or something else,
even), and in the string as \n. It's that simple.

ChrisA



More information about the Python-list mailing list