String to sequence

mattia gervaz at gmail.com
Sat Mar 14 06:50:49 EDT 2009


Il Sat, 14 Mar 2009 10:35:59 +0100, Peter Otten ha scritto:

> mattia wrote:
> 
>> How can I convert the following string:
>> 
>> 'AAR','ABZ','AGA','AHO','ALC','LEI','AOC',
>> EGC','SXF','BZR','BIQ','BLL','BHX','BLQ'
>> 
>> into this sequence:
>> 
>> ['AAR','ABZ','AGA','AHO','ALC','LEI','AOC',
>> EGC','SXF','BZR','BIQ','BLL','BHX','BLQ']
> 
>>>> s = "'AAR','ABZ','AGA','AHO','ALC','LEI','AOC'"
>>>> csv.reader(StringIO.StringIO(s), quotechar="'").next()
> ['AAR', 'ABZ', 'AGA', 'AHO', 'ALC', 'LEI', 'AOC']
> 
> or
> 
>>>> s = "'AAR','ABZ','AGA','AHO','ALC','LEI','AOC'" list(compile(s,
>>>> "nofile", "eval").co_consts[-1])
> ['AAR', 'ABZ', 'AGA', 'AHO', 'ALC', 'LEI', 'AOC']
> 
> Peter

Ok, and what about if the string is "['AAR', 'ABZ', 'AGA', 'AHO', 'ALC']" 
I wanted to use eval(string) but it is discouraged, they say.



More information about the Python-list mailing list