Re: convert a string to tuple

2005-06-02 Thread Steven D'Aprano
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 ValueErro

Re: convert a string to tuple

2005-05-31 Thread flamesrock
lol -- http://mail.python.org/mailman/listinfo/python-list

Re: convert a string to tuple

2005-05-31 Thread Peter Hansen
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 -- http://mail.python.org/mailman/listinfo/python-l

Re: convert a string to tuple

2005-05-31 Thread Steven Bethard
[EMAIL PROTECTED] 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 -- http://mail.python.org/m

Re: convert a string to tuple

2005-05-31 Thread Steven Bethard
[EMAIL PROTECTED] 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 o

Re: convert a string to tuple

2005-05-31 Thread [EMAIL PROTECTED]
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 -- http://mail.python.org/mailman/listinfo/python-list

Re: convert a string to tuple

2005-05-31 Thread Marc 'BlackJack' Rintsch
In <[EMAIL PROTECTED]>, 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,

convert a string to tuple

2005-05-31 Thread querypk
how do I convert b is a string b = '(1,2,3,4)' to b = (1,2,3,4) -- http://mail.python.org/mailman/listinfo/python-list