简体   繁体   English

将字符串转换为python中的列表

[英]Converting string to list in python

我正在尝试使用json.loader将此字符串转换为python列表,但它不起作用。

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

JSON expects strings to be wrapped with double quotes; JSON期望字符串用双引号引起来; 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: 如果您知道可以将double替换为single,则可以执行以下操作:

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]]

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. 但是, eval执行赋予它的所有操作,因此请确保您的数据不来自恶意源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM