472,354 Members | 2,105 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,354 software developers and data experts.

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 2906
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Tim | last post by:
Hi I want to be able to split the contents of a text field into two or maybe three columns. The text field contains text AND HTML mark-up. My initial thought was to find the middle character...
4
by: qwweeeit | last post by:
The standard split() can use only one delimiter. To split a text file into words you need multiple delimiters like blank, punctuation, math signs (+-*/), parenteses and so on. I didn't...
8
by: Xah Lee | last post by:
i have a large number of lines i want to turn into a list. In perl, i can do @corenames=qw( rb_basic_islamic sq1_pentagonTile sq_arc501Tile sq_arc503Tile );
1
by: Raed Sawalha | last post by:
I havea regular expression to text as pairs key:value (?<Keyword>\w+):(?<Value>.*)((?=\W$)|\z) when enter the text as following: x-sender: raed_sawalha@hotmail.com x-receiver:...
3
by: Microsoft | last post by:
I have a multine list that I would like to split into an array. I paste it into a richtext box and go from there, but it just makes the first part of the array the whole list with little boxes...
2
by: ownowl | last post by:
Hello beginer under python, I have a problem to get lines in a text file. lines have inside the \n (x0A) char, and le readline method split the line at this char too (not only at x0Dx0A). for...
0
by: Eric | last post by:
Visual C++ 2005 Express MVP's and experience programmer's only please!... I need to get the number of lines in a textbox so I can insert them into a listview. The text comes from my database...
3
by: ashok | last post by:
Hi, I need a function that will divide text from mysql in 2 parts, so that I can display first half in one column and second half in second column. I can't find what function will do this job....
1
by: sirrobert | last post by:
Hey everyone. I've got some DIV elements containing arbitrary HTML that is entirely text (some of the childNodes are textNodes, some are strong or em tags containing textNodes, some are divs or spans...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made but the http to https rule only works for...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. header("Location:".$urlback); Is this the right layout the...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.