How convert a list string to a real list

Tim Chase python.list at tim.thechases.com
Wed Nov 30 18:25:46 EST 2011


On 11/30/11 16:48, Hidura wrote:
> Why you don't make this "['1','2','3']".strip("[]").split(',') work for me

because it breaks on things like

   s = """
     [[1,2,3],42,'''triple the fun!''', "can't touch this, 
eh?",r'"Back\\\slashes?!", she said.', [4,5,6]]
     """

Commas can be embedded in strings, strings can be delimited with 
single, double, or triple quotes, raw strings can be used, more 
than one opening or closing "["/"]" can appear in a row, leading 
or trailing whitespace might throw you off...using 
ast.literal_eval() should just Do the Right Thing™.

-tkc





More information about the Python-list mailing list