How do I parse a string to a tuple??

Discussion in 'Python' started by Soren, Apr 30, 2007.

  1. Soren

    Soren Guest

    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
    Soren, Apr 30, 2007
    #1
    1. Advertising

  2. Soren

    James Stroud Guest

    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
    James Stroud, Apr 30, 2007
    #2
    1. Advertising

  3. 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
    Steven D'Aprano, Apr 30, 2007
    #3
  4. On Apr 30, 5:47 pm, 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?
    >
    > Thanks!,
    > Soren


    tuple("text1 \n text2 \n text3 \n text4".split('\n'))
    =?utf-8?B?5Lq66KiA6JC95pel5piv5aSp5rav77yM5pyb5p6B, Apr 30, 2007
    #4
  5. 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
    Michael Hoffman, Apr 30, 2007
    #5
  6. Soren

    Soren Guest

    Thanks alot everyone!

    Soren
    Soren, Apr 30, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michal Mikolajczyk
    Replies:
    1
    Views:
    803
    Larry Bates
    Apr 20, 2004
  2. Jeff Epler
    Replies:
    0
    Views:
    949
    Jeff Epler
    Apr 20, 2004
  3. Bill Scherer
    Replies:
    0
    Views:
    610
    Bill Scherer
    Apr 20, 2004
  4. Gregor Horvath

    Why tuple with one item is no tuple

    Gregor Horvath, Mar 15, 2005, in forum: Python
    Replies:
    37
    Views:
    818
    Antoon Pardon
    Mar 30, 2005
  5. Replies:
    19
    Views:
    1,119
    Daniel Vallstrom
    Mar 15, 2005
Loading...

Share This Page