Convert string to list. Python [string.split() acting weird]

Use ast.literal_eval():

Safely evaluate an expression node or a Unicode or Latin-1 encoded
string containing a Python expression. The string or node provided may
only consist of the following Python literal structures: strings,
numbers, tuples, lists, dicts, booleans, and None.

>>> from ast import literal_eval
>>> temp = "['a','b','c']"
>>> l = literal_eval(temp)
>>> l
['a', 'b', 'c']
>>> type(l)
<type 'list'>

Leave a Comment