Python: Lista representada em string para lista

 

In [1]: import ast


In [2]: import json


In [3]: x = '[ "A","B","C" , " D"]'


In [4]: ast.literal_eval(x)

Out[4]: ['A', 'B', 'C', ' D']


In [5]: json.loads(x)

Out[5]: ['A', 'B', 'C', ' D']


In [6]: [a.strip() for a in json.loads(x)]

Out[6]: ['A', 'B', 'C', 'D']


In [7]: [a.strip() for a in ast.literal_eval(x)]

Out[7]: ['A', 'B', 'C', 'D']


Comentários