NEWLINE character problem

Tim Peters tim.one at comcast.net
Fri Jan 30 23:30:28 EST 2004


[Dragos Chirila]
>> I have a string and I want to split it by NEWLINE character.
>>
>> It is knowned that "Microsoft Windows, Apple's Macintosh OS and
>> various UNIX, all use different characters to mark the ends of lines
>> in text files. Unix uses the linefeed (ASCII character 10), MacOS
>> uses the carriage return (ASCII character 13), and Windows uses a
>> two-character sequence of a carriage return plus a newline". So,
>> PLEASE don't tell me to use 'split' by '\n' because it won't work.

[Jp Calderone]
> s.splitlines()

Woo hoo!  Jp is right, and I never realized that splitlines() looks for all
of \n, \r\n, and \r line endings.

>>> 'a\nb\rc\r\n'.splitlines()
['a', 'b', 'c']
>>> 'a\nb\rc\r\n'.splitlines(True)
['a\n', 'b\r', 'c\r\n']
>>>

That's certainly Pythonic <wink>.





More information about the Python-list mailing list