K
kj
In Perl, one can break a chunk of text into an array of lines while
preserving the trailing line-termination sequence in each line, if
any, by splitting the text on the regular expression /^/:
DB<1> x split(/^/, "foo\nbar\nbaz")
0 'foo
'
1 'bar
'
2 'baz'
But nothing like this seems to work in Python:
['foo\nbar\nbaz']
(One gets the same result if one adds the re.MULTILINE flag to the
re.split call.)
What's the Python idiom for splitting text into lines, preserving
the end-of-line sequence in each line?
preserving the trailing line-termination sequence in each line, if
any, by splitting the text on the regular expression /^/:
DB<1> x split(/^/, "foo\nbar\nbaz")
0 'foo
'
1 'bar
'
2 'baz'
But nothing like this seems to work in Python:
['foo\nbar\nbaz']
(One gets the same result if one adds the re.MULTILINE flag to the
re.split call.)
What's the Python idiom for splitting text into lines, preserving
the end-of-line sequence in each line?