Re: How do I parse a string to a tuple??
- From: Steven D'Aprano <steve@xxxxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Mon, 30 Apr 2007 20:06:43 +1000
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
.
- Follow-Ups:
- Re: How do I parse a string to a tuple??
- From: Michael Hoffman
- Re: How do I parse a string to a tuple??
- References:
- How do I parse a string to a tuple??
- From: Soren
- How do I parse a string to a tuple??
- Prev by Date: Re: How do I parse a string to a tuple??
- Next by Date: Re: Dict Copy & Compare
- Previous by thread: Re: How do I parse a string to a tuple??
- Next by thread: Re: How do I parse a string to a tuple??
- Index(es):
Relevant Pages
|