0

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]]"
3
  • 1
    How are you trying to do this? Post the code please Commented Aug 22, 2018 at 14:57
  • Hi Juan. It looks like you want to convert a string to a list. Is that correct? But your string does not look like it is in correct json format. Commented Aug 22, 2018 at 15:03
  • i trying first using the list function , that is: data="[[0,0,1,'Regulado',0,0,0],[0,0,1,'Nivel 1',0,0,0]]" list(data). but that divides me character by character Commented Aug 22, 2018 at 15:03

2 Answers 2

2

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]]
Sign up to request clarification or add additional context in comments.

Comments

0

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.

3 Comments

import ast; ast.literal_eval(...)
i would say json is a better option here due to the dangers of eval
@modesitt: I would say that too, just wan't sure it would parse it right.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.