python - 如何从列中获取属性高度、宽度、x 和 y?
问题描述
我们有 1000 行具有相同信息的 CSV 文件存储在一列中,如下所示。我们如何循环它并在 python 中获取属性 x、y、height 和 width?
[{"task":"T0","task_label":"Draw a box around each person name and transcribe their information.","value":[{"x":224.63333129882812,"y":89.96666717529297,"tool":0,"frame":0,"width":333.9999694824219,"height":42.00000762939453,"details":[{"value":"Rev. Leopold Wyke Acland"},{"value":0}],"tool_label":"Tool name"},{"x":95.63333129882812,"y":55.96666717529297,"tool":0,"frame":0,"width":280,"height":37,"details":[{"value":"Acland, Thomas Wyke"},{"value":0}],"tool_label":"Tool name"}]}]
解决方案
由于最外层list
仅包含一个具有 3 个字段的对象
task
不需要。task_label
不需要。value
我们感兴趣的一个。
在给定的上下文中,我们可以通过dict
以下方式循环
for item in data[0]["value"]:
print(f"x = {item['x']}")
print(f"y = {item['y']}")
print(f"width = {item['width']}")
print(f"height = {item['height']}")
您还可以以更 Python 的方式收集这些值,然后遍历结果的字典列表
result = [
{
"x": item["x"],
"y": item["y"],
"width": item["width"],
"height": item["height"]
}
for item in data[0]["value"]]
我建议在 python 中进行迭代。此外,此代码段假定所有行都具有统一的结构。
推荐阅读
- powershell - 在 Powershell 文件中,捕获少数异常并忽略少数异常
- java - jdbctemplate - queryForList - 传递日期?
- javascript - 如何在 window.open 函数中传递 php 变量
- ios - 为什么@unknown default (Swift 5) 会导致编译错误?
- python - 在 django 中解析多个 sql 输出记录(使用游标对象)
- ios - TestFlight 不显示任何 iOS 构建
- java - 使用java nsis脚本安装安装程序后如何以管理员身份运行.exe文件
- c# - 为什么我的 Azure Function V2 没有记录到应用洞察?
- java - 如何自定义zendesk聊天消息设计?安卓版
- hyperledger-fabric - ENDORSEMENT_FAILURE 同时通过 putState 为 3 Orgs 网络创建键值