473,395 Members | 1,948 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 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 2966
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.