convert a string to tuple

Discussion in 'Python' started by querypk@gmail.com, May 31, 2005.

  1. Guest

    how do I convert
    b is a string b = '(1,2,3,4)' to b = (1,2,3,4)
    , May 31, 2005
    #1
    1. Advertising

  2. Guest

    Pass it to eval:

    >>> eval('(1, 2, 3, 4, 5)')

    (1, 2, 3, 4, 5)

    Basically what you are doing it evaluating the repr of the tuple.

    -Brett
    , May 31, 2005
    #2
    1. Advertising

  3. wrote:
    > b is a string b = '(1,2,3,4)' to b = (1,2,3,4)


    py> tuple(int(s) for s in '(1,2,3,4)'[1:-1].split(','))
    (1, 2, 3, 4)

    Or if you're feeling daring:

    py> eval('(1,2,3,4)', dict(__builtins__=None))
    (1, 2, 3, 4)

    Python makes no guarantees about the security of this second one though.

    STeVe
    Steven Bethard, May 31, 2005
    #3
  4. In <>, querypk wrote:

    > how do I convert
    > b is a string b = '(1,2,3,4)' to b = (1,2,3,4)


    In [1]: b = '(1,2,3,4)'

    In [2]: b[1:-1]
    Out[2]: '1,2,3,4'

    In [3]: b[1:-1].split(',')
    Out[3]: ['1', '2', '3', '4']

    In [4]: tuple(b[1:-1].split(','))
    Out[4]: ('1', '2', '3', '4')

    Ooops, you wanted ints in there:

    In [5]: tuple(map(int, b[1:-1].split(',')))
    Out[5]: (1, 2, 3, 4)

    Ciao,
    Marc 'BlackJack' Rintsch
    Marc 'BlackJack' Rintsch, May 31, 2005
    #4
  5. wrote:
    > Pass it to eval:
    >
    >>>>eval('(1, 2, 3, 4, 5)')

    > (1, 2, 3, 4, 5)


    Just be sure you know where your strings come from. You wouldn't want
    someone to pass you
    """__import__('os').system('rm -rf /')"""
    and then send that to eval. =)

    STeVe
    Steven Bethard, May 31, 2005
    #5
  6. Peter Hansen Guest

    Steven Bethard wrote:
    > Just be sure you know where your strings come from. You wouldn't want
    > someone to pass you
    > """__import__('os').system('rm -rf /')"""
    > and then send that to eval. =)


    Why not, Steven? I just tried it and my compu
    Peter Hansen, May 31, 2005
    #6
  7. flamesrock Guest

    lol
    flamesrock, May 31, 2005
    #7
  8. On Tue, 31 May 2005 13:14:09 -0700, querypk wrote:

    > how do I convert
    > b is a string b = '(1,2,3,4)' to b = (1,2,3,4)


    You can do:

    def str2tuple(s):
    """Convert tuple-like strings to real tuples.
    eg '(1,2,3,4)' -> (1, 2, 3, 4)
    """
    if s[0] + s[-1] != "()":
    raise ValueError("Badly formatted string (missing brackets).")
    items = s[1:-1] # removes the leading and trailing brackets
    items = items.split(',')
    L = [int(x.strip()) for x in items] # clean up spaces, convert to ints
    return tuple(L)

    For real production code, you will probably want better error checking.

    --
    Steven.
    Steven D'Aprano, Jun 2, 2005
    #8
    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:
    788
    Larry Bates
    Apr 20, 2004
  2. Jeff Epler
    Replies:
    0
    Views:
    916
    Jeff Epler
    Apr 20, 2004
  3. Bill Scherer
    Replies:
    0
    Views:
    599
    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:
    783
    Antoon Pardon
    Mar 30, 2005
  5. Steve
    Replies:
    1
    Views:
    939
    Fredrik Lundh
    Dec 13, 2005
Loading...

Share This Page