-2

Quick question, some data was corrupted and now all the lists I had have turned into strings:

e.g: ["en", "ru"],

is now :

str(["en", "ru"])

What is the best way to turn it back to list?

Inconnu
  • 4,928
  • 2
  • 31
  • 41
ivan_bilan
  • 2,041
  • 3
  • 26
  • 51
  • Found a duplicate: http://stackoverflow.com/questions/1894269/convert-string-representation-of-list-to-list-in-python Thanks everyone! – ivan_bilan Dec 09 '16 at 09:07

2 Answers2

4

Use ast.literal_eval. Do not use eval it is unsafe.

>>> import ast
>>> ast.literal_eval(str(["en", "ru"]))
['en', 'ru']
Chris_Rands
  • 30,797
  • 12
  • 66
  • 100
1

eval(str(["a", "b"]))

gets you ["a", "b"]

jf328
  • 5,194
  • 6
  • 44
  • 71