Converting string to list in python

I am trying convert this string to a python list using json.loader but it does not work.

"[[0,0,1,'Regulado',0,0,0],[0,0,1,'Nivel 1',0,0,0]]"

Upvotes: 0

Views: 77

Answers (2)

chrisaycock
chrisaycock

Reputation: 37928

JSON expects strings to be wrapped with double quotes; the single quotes in your example are causing the issue. If you know that you can just replace single with double, then you can do this:

In [26]: import json

In [27]: json.loads(s.replace("'", '"'))
Out[27]: [[0, 0, 1, 'Regulado', 0, 0, 0], [0, 0, 1, 'Nivel 1', 0, 0, 0]]

Upvotes: 2

blue note
blue note

Reputation: 29099

The simplest way is

x = eval("[[0,0,1,'Regulado',0,0,0],[0,0,1,'Nivel 1',0,0,0]]")

However, eval executes anything that is given to it, so be sure that your data don't come from malicious sources.

Upvotes: 0

Related Questions