468,287 Members | 1,898 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

How do I parse a string to a tuple??

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

Apr 30 '07 #1
5 1631
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

Apr 30 '07 #2
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
Apr 30 '07 #3
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'))

Apr 30 '07 #4
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
Apr 30 '07 #5

Thanks alot everyone!

Soren

Apr 30 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Alastair G. Hogge | last post: by
7 posts views Thread by Kamilche | last post: by
16 posts views Thread by flyaflya | last post: by
25 posts views Thread by John Salerno | last post: by
11 posts views Thread by Stef Mientki | last post: by
2 posts views Thread by MrBee | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.