STRING TO LIST

Anton Muhin antonmuhin at sendmail.ru
Tue Apr 8 14:44:17 EDT 2003


jubafre at brturbo.com wrote:
> Hello, 
> 
> I need to change a variable from string to list. 
> 
>>>>>x=str("[[12,13],[14,15]]") 
>>>>>x 
>>
>>'[[12,13],[14,15]]' 
>>
>>i have x and i want to change to a list, but not in this way: 
>>
>>
>>>>>x=list(x) 
>>>>>x 
>>
>>['[', '[', '1', '2', ',', '1', '3', ']', ',', '[', '1', '4', ',', '1', '5', ']', ']'] 
>>
>>must be a list like this: 
>>
>>
>>>>>x=[[12,13],[14,15]] 
>>>>>x 
>>
>>[[12, 13], [14, 15]] 
>>
>>someone know??? 
> 
> 
> 
> Juliano Freitas

Use eval:
PythonWin 2.2.1 (#34, Apr 15 2002, 09:51:39) [MSC 32 bit (Intel)] on win32.
Portions Copyright 1994-2001 Mark Hammond (mhammond at skippinet.com.au) - 
see 'Help/About PythonWin' for further copyright information.
 >>> s = str([[1, 2], [3, 4]])
 >>> s
'[[1, 2], [3, 4]]'
 >>> l = eval(s)
 >>> l
[[1, 2], [3, 4]]
 >>>

hth,
Anton.





More information about the Python-list mailing list