Python3 - 将复杂的字符串转换为列表/字典

标签 python python-3.x list dictionary

<分区>

我有这样一个字符串:

l1="[{'t_steps': '', 't_expected': '', 't_type': 'Functional', 't_precond': 'xxx', 't_notes': 'test note', 't_name': 'First test'}]"

我需要将它转换成一个真正的列表对象,例如:

l1=[{'t_steps': '', 't_expected': '', 't_type': 'Functional', 't_precond': 'xxx', 't_notes': 'test note', 't_name': 'First test'}]

我试过:

l1=list(l1)

l1=l1.split(',')

但是结果并不好。 请有人帮我把我的字符串转换成 python 可以读取的形式吗?

非常感谢

最佳答案

使用eval

l1="[{'t_steps': '', 't_expected': '', 't_type': 'Functional', 't_precond': 'xxx', 't_notes': 'test note', 't_name': 'First test'}]"

这样做:

l1 = eval(l1)
l1

输出:

[{'t_steps': '', 't_expected': '', 't_type': 'Functional', 't_precond': 'xxx', 't_notes': 'test note', 't_name': 'First test'}]

关于Python3 - 将复杂的字符串转换为列表/字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57881725/

相关文章:

python - 如何读取脚本中的标准输入?

python - 如何在我自己的应用程序中加载 NPAPI 插件?

c# - 按 id 区分包含对象的列表

list - Prolog: between/3 最后有一个列表

java - 在 hibernate 中将列表存储为可序列化对象

python - 不修改 Django wsgi.conf 的 AWS ElasticBeanstalk 更新

python - 如何保存 Keras 的训练历史以进行交叉验证(循环)?

Python:如何从列表列表中删除包含 Nones 的列表?

Python-无法将文件上传到AWS S3存储桶中定义的文件夹

python-3.x - 如何删除图例的特定部分(seaborn,散点图)