how to split text into lines?

kj socyl at 987jk.com.invalid
Wed Jul 30 16:45:57 EDT 2008


In <g6qjmc$f2o$1 at reader1.panix.com> kj <socyl at 987jk.com.invalid> writes:



>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:

>>>> re.split('^', 'foo\nbar\nbaz')
>['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?


Sorry, I should have googled this first.  I just found splitlines()...

Still, for my own edification, is there a way to achieve the same
effect using re.split?

TIA!

kynn

-- 
NOTE: In my address everything before the first period is backwards;
and the last period, and everything after it, should be discarded.



More information about the Python-list mailing list