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