string to list when the contents is a list
vlastimil.brom at gmail.com
Thu Feb 18 01:08:32 CET 2010
2010/2/18 Wes James <comptekki at gmail.com>:
> I have been trying to create a list form a string. The string will be
> a list (this is the contents will look like a list). i.e. "" or
> The "" is simple since I can just check if value == "" then return 
> But with "['a','b']" I have tried and get:
> [ " 'a' "," 'b' " ]
> when I want it to return ['a','b'].
> How can I do this?
The potentially problematic exec or eval options left aside,
if you really need to do this, you might consider pyparsing; check the example
If you know, the input string will always have this exact format
(single quoted comma separated one-character strings between square
brackets), you might use regular expressions to some extent, e.g.
print re.findall(r"(?<=')\w(?=')", "['a','b','c','b','A']")
['a', 'b', 'c', 'b', 'A']
More information about the Python-list