By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,332 Members | 1,057 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,332 IT Pros & Developers. It's quick & easy.

how to split text into lines?

P: n/a
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<1x 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?
--
NOTE: In my address everything before the first period is backwards;
and the last period, and everything after it, should be discarded.
Jul 30 '08 #1
Share this Question
Share on Google+
5 Replies

P: n/a
kj
In <g6**********@reader1.panix.comkj <so***@987jk.com.invalidwrites:
>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<1x 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.
Jul 30 '08 #2

P: n/a
On Wed, Jul 30, 2008 at 4:45 PM, kj wrote:
>>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?
Not directly: re.split doesn't split on zero-length matches.

http://mail.python.org/pipermail/pyt...st/047272.html
http://bugs.python.org/issue852532
http://bugs.python.org/issue988761
http://bugs.python.org/issue3262

-Miles
Jul 30 '08 #3

P: n/a
kj wrote:
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?
re.split(os.linesep, <string>) works the same as <string>.splitlines()

Neither retain the EOL for each line, though. The only way I'm aware
of is to re-add it:

[s+os.linesep for s in re.split(os.linesep, <string>)]

Was that what you were after?
Jul 31 '08 #4

P: n/a
On Jul 31, 7:26*am, alex23 <wuwe...@gmail.comwrote:
kj wrote:
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?

re.split(os.linesep, <string>) works the same as <string>.splitlines()

Neither retain the EOL for each line, though. The only way I'm aware
of is to re-add it:

[s+os.linesep for s in re.split(os.linesep, <string>)]

Was that what you were after?
or what about 'string'.splitlines(True) as that retains newline
characters. ;)
Jul 31 '08 #5

P: n/a
Chris wrote:
or what about 'string'.splitlines(True) as that retains newline
characters. ;)
Okay, you win :)

Man, you'd think with the ease of object introspection I'd have at
least looked at its docstring :)

Cheers, Chris!
Aug 1 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.