Hi!
I have a string that contains some text and newline characters. I want
to parse the string so that the string just before a newline character
goes in as an element in the tuple.
ex:
"text1 \n text2 \n text3 \n text4" --(text1, text2, text3, text4)
Is there an easy way to do this?
Thanks!,
Soren 5 1757
On Mon, 30 Apr 2007 02:47:32 -0700, Soren wrote:
Hi!
I have a string that contains some text and newline characters. I want
to parse the string so that the string just before a newline character
goes in as an element in the tuple.
ex:
"text1 \n text2 \n text3 \n text4" --(text1, text2, text3, text4)
Is there an easy way to do this?
the_string = "text1 \n text2 \n text3 \n text4"
tuple(the_string.split('\n'))
If you don't need a tuple, and a list will do:
the_string.split('\n')
If you want to get rid of the white space after each chunk of text:
[s.strip() for s in the_string.split('\n')]
--
Steven D'Aprano
Soren wrote:
Hi!
I have a string that contains some text and newline characters. I want
to parse the string so that the string just before a newline character
goes in as an element in the tuple.
ex:
--(text1, text2, text3, text4)
Is there an easy way to do this?
Thanks!,
Soren
For this particular, very narrow, example, following the example as
closely as I possibly can:
import re
atext = "text1 \n text2 \n text3 \n text4"
atup = tuple(re.split(r'\s*\n', atext))
James
On Apr 30, 5:47Â*pm, Soren <soren.skou.niel...@gmail.comwrote:
Hi!
I have a string that contains some text and newline characters. I want
to parse the string so that the string just before a newline character
goes in as an element in the tuple.
ex:
"text1 \n text2 \n text3 \n text4" Â* --(text1, text2, text3, text4)
Is there an easy way to do this?
Thanks!,
Soren
tuple("text1 \n text2 \n text3 \n text4".split('\n'))
Steven D'Aprano wrote:
On Mon, 30 Apr 2007 02:47:32 -0700, Soren wrote:
>"text1 \n text2 \n text3 \n text4" --(text1, text2, text3, text4)
the_string = "text1 \n text2 \n text3 \n text4"
tuple(the_string.split('\n'))
If you don't need a tuple, and a list will do:
the_string.split('\n')
or the_string.splitlines()
If you want to get rid of the white space after each chunk of text:
[s.strip() for s in the_string.split('\n')]
--
Michael Hoffman
Thanks alot everyone!
Soren This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Alastair G. Hogge |
last post by:
Hello *,
I'm using Python and the cgi module to retrive data from a HTML form.
I'm then trying to get that information into a string. But efforts fail when
I run type(foo) on my string. type()...
|
by: chuck amadi |
last post by:
By the way list is there a better way than using the readlines() to
> > >parse the mail data into a file , because Im using
> > >email.message_from_file it returns
> > >all the data i.e reads one...
|
by: Kamilche |
last post by:
I want to convert a dict into string form, then back again. After
discovering that eval is insecure, I wrote some code to roll a Python
object, dict, tuple, or list into a string. I've posted it...
|
by: flyaflya |
last post by:
a = "(1,2,3)"
I want convert a to tuple:(1,2,3),but tuple(a) return ('(', '1', ',',
'2', ',', '3', ')') not (1,2,3)
|
by: Amy L. |
last post by:
I have a class that contains a string array. However, I can't get
this object to serialize in the xml output. Is there a trick to get a
string to serialize?
Thanks
Amy.
|
by: Jon Bowlas |
last post by:
Hi listers,
I wrote this script in Zope some time ago and it worked for a while, but now
I'm getting the following error:
TypeError: coercing to Unicode: need string or buffer, NoneType found
...
|
by: John Salerno |
last post by:
Forgive my excitement, especially if you are already aware of this, but
this seems like the kind of feature that is easily overlooked (yet could
be very useful):
Both 8-bit and Unicode strings...
|
by: Stef Mientki |
last post by:
hello,
I need to translate the following string
a = '(0, 0, 0, 255), (192, 192, 192, 255), True, 8'
into the following list or tuple
b =
Is there a simple way to to this.
(Not needed...
|
by: Tim Chase |
last post by:
Is there an easy way to make string-formatting smart enough to
gracefully handle iterators/generators? E.g.
transform = lambda s: s.upper()
pair = ('hello', 'world')
print "%s, %s" % pair #...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |