Re: convert a string to tuple



In <1117570449.887422.225670@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>, 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
.



Relevant Pages