Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
字符串格式的列表,要转换为列表列表(Python)_Python_String_List - Fatal编程技术网

字符串格式的列表,要转换为列表列表(Python)

字符串格式的列表,要转换为列表列表(Python),python,string,list,Python,String,List,我收到一条消息,它是一个列表列表,但在Python中被识别为字符串。我不知道如何让Python将字符串解释为列表列表,有简单的方法吗?我正在处理的格式示例如下所示: >>soc = '[["hello","world"],["foo","bar"]]' >>type(soc) <type 'str'> >soc='[[“你好”,“世界”],[“foo”,“bar”]' >>类型(soc) 我想将此字符串转换为未更改的列表列表: >>soc [[

我收到一条消息,它是一个列表列表,但在Python中被识别为字符串。我不知道如何让Python将字符串解释为列表列表,有简单的方法吗?我正在处理的格式示例如下所示:

>>soc = '[["hello","world"],["foo","bar"]]'
>>type(soc)
<type 'str'>
>soc='[[“你好”,“世界”],[“foo”,“bar”]'
>>类型(soc)
我想将此字符串转换为未更改的列表列表:

>>soc
[["hello","world"],["foo","bar"]]
>>type(soc)
<type 'list'>
soc [[“你好”,“世界”],[“福”,“酒吧”]] >>类型(soc) 谢谢你的帮助,谢谢

使用:

你可以用


为什么不使用内置的
eval
功能

soc = eval('[["hello","world"],["foo","bar"]]')
type(soc)
Out[5]: list

soc
Out[6]: [['hello', 'world'], ['foo', 'bar']]

soc[1][0]
Out[7]: 'foo'
现在,关于“安全性”的预期异议…

重复?可能重复的
>>> import json
>>> json.loads(soc)
[['hello', 'world'], ['foo', 'bar']]
soc = eval('[["hello","world"],["foo","bar"]]')
type(soc)
Out[5]: list

soc
Out[6]: [['hello', 'world'], ['foo', 'bar']]

soc[1][0]
Out[7]: 'foo'