469,908 Members | 2,213 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,908 developers. It's quick & easy.

how to split text into lines?

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
5 2787
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
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
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
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
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.

Similar topics

2 posts views Thread by Tim | last post: by
4 posts views Thread by qwweeeit | last post: by
8 posts views Thread by Xah Lee | last post: by
1 post views Thread by Raed Sawalha | last post: by
3 posts views Thread by Microsoft | last post: by
2 posts views Thread by ownowl | last post: by
reply views Thread by Eric | last post: by
3 posts views Thread by ashok | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.