python-3.x - 字符串格式的 Pandas 列表到 dict 列表
问题描述
我有一个数据框,其中有一列名为 coor,它在列表中具有 dict 作为字符串。
df['coor'][0]
'[{'x': 720.65215, 'y': 636.51048, 'width': 332.19348, 'height': 648.12561}, {'x': 2044.77989, 'y': 847.90622, 'width': 329.87049, 'height': 576.11169}]'
我尝试了一些项目,如 json 加载、字符串到列表的转换。没有结果。输出
[
{'x': 720.65215, 'y': 636.51048, 'width': 332.19348, 'height': 648.12561},
{'x': 2044.77989, 'y': 847.90622, 'width': 329.87049, 'height': 576.11169}
]
解决方案
对于我ast.literal_eval
在这个示例数据中工作:
import ast
#changed not valid `''` to `""` around string
a = "[{'x': 720.65215, 'y': 636.51048, 'width': 332.19348, 'height': 648.12561}, {'x': 2044.77989, 'y': 847.90622, 'width': 329.87049, 'height': 576.11169}]"
print (ast.literal_eval(a))
[{'x': 720.65215, 'y': 636.51048, 'width': 332.19348, 'height': 648.12561},
{'x': 2044.77989, 'y': 847.90622, 'width': 329.87049, 'height': 576.11169}]
推荐阅读
- jquery - 为什么引导选择选择器无法第二次调用?
- linux - 如何计算特定列有两种模式的行数?
- uwp - UWP 选取框文本动画
- sql - 在 Waterline ORM 上的同一查询中使用同一列模型
- c# - 文件上传 .NET Core 'IFormFile' 不包含 'CopyToAsync' 的定义,也没有扩展方法
- ios - 从 Jenkins Xcode 插件构建 iOS 失败
- vim - 在不安装任何插件的情况下创建 Vim 片段?
- android - 我无法打开 flutter_console.bat 文件
- pytest - 我怎样才能使输出不那么冗长
- hibernate - 嵌套异常是 org.h2.jdbc.JdbcSQLException:找不到列“COMMENT”;SQL 语句: