how can I convert a string representation of a list of lists to a list data structure in python. For example if I have a string k
:
k="['A',['B','C'],'D']"
The desired output I want is a list like below
['A',
['B','C'],
'D']
CodePudding user response:
Use ast.literal_eval()
:
>>> import ast
>>> ast.literal_eval(k)
['A', ['B', 'C'], 'D']
CodePudding user response:
eval
is a built-in python function that
parses the given string argument and evaluates it as an expression.
>>> k = "['A',['B','C'],'D']"
>>> print(eval(k))
['A', ['B', 'C'], 'D']