String to sequence

Peter Otten __peter__ at web.de
Sat Mar 14 05:35:59 EDT 2009


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




More information about the Python-list mailing list