[Tutor] Turning a string into a tuple?

Ignacio Vazquez-Abrams ignacio@openservices.net
Thu, 27 Sep 2001 01:40:19 -0400 (EDT)


On Thu, 27 Sep 2001, David L. Lerner wrote:

> I need to turn a string:
>
> "['Alda', 11, 9, 6, 'Trey', 10, 12]"
>
> into a tuple:
>
> ['Alda', 11, 9, 6, 'Trey', 10, 12]
>
> This seems like it should be simple, sort of  the reverse of  string (), but
> I can't find how to do it.  I tried:
>
> >>> tuple ("['Alda', 11, 9, 6, 'Trey', 10, 12]")
>
> and I got:
>
> ('[', "'", 'A', 'l', 'd', 'a', "'", ',', ' ', '1', '1', ',', ' ', '9', ',',
> ' ', '6', ',', ' ', "'", 'T', 'r', 'e', 'y', "'", ',', ' ', '1', '0', ',', '
> ', '1', '2', ']')

Use eval() to turn the string into a list, then use tuple() to turn the list
into a tuple.

-- 
Ignacio Vazquez-Abrams  <ignacio@openservices.net>