string to list when the contents is a list

Wes James comptekki at
Thu Feb 18 20:32:59 CET 2010

On Thu, Feb 18, 2010 at 8:18 AM, Tim Chase
<python.list at> wrote:
> Wes James wrote:

> Just to add to the list of solutions I've seen, letting the built-in csv
> module do the heavy lifting:
>  >>> s = "['a','b']"
>  >>> import csv
>  >>> no_brackets = s[1:-1] # s.strip(' \t[]')
>  >>> c = csv.reader([no_brackets], quotechar="'")
>  >>>
>  ['a', 'b']
> This also gives you a bit of control regarding how escaping is done, and
> other knobs & dials to twiddle if you need. Additionally, if you have more
> than one string to process coming from an iterable source (such as a file),
> you can just pass that iterator to csv.reader() instead of concocting a
> one-element list.

Thx,  I think this will work for what I want.


More information about the Python-list mailing list