python - 处理来自 json 的数据以反映每个条目的单个值
问题描述
设置:
这个数据集有 50 个“问题”,在这些“问题”中,我已经捕获了我需要然后放入我的 postgresql 数据库的数据。但是当我到达“组件”时,我遇到了麻烦。我能够获得“组件”的所有“名称”的列表,但只想为每个“问题”拥有 1 个“名称”实例,其中一些有 2 个。有些是空的,想为那些。
以下是一些应该足够的示例数据:
{
"issues": [
{
"key": "1",
"fields": {
"components": [],
"customfield_1": null,
"customfield_2": null
}
},
{
"key": "2",
"fields": {
"components": [
{
"name": "Testing"
}
],
"customfield_1": null,
"customfield_2": null
}
},
{
"key": "3",
"fields": {
"components": [
{
"name": "Documentation"
},
{
"name": "Manufacturing"
}
],
"customfield_1": null,
"customfield_2": 5
}
}
]
}
我正在寻找返回(仅用于组件名称):
['null', 'Testing', 'Documentation']
我设置了其他数据以进入数据库,如下所示:
values = list((item['key'],
//components list,
item['fields']['customfield_1'],
item['fields']['customfield_2']) for item in data_story['issues'])
我想知道是否有一种可能的方法可以进入我在上面评论过“组件列表”的已创建组件列表中
只是为了回顾一下,我希望每个问题只有一个组件名称是否为空,并且能够将其values
与其余数据一起放入变量中。组件中的第一个name
也适用于每个“问题”
解决方案
假设我们正在使用一个data
变量,这就是我要做的:
values = [(x['fields']['components'][0]['name'] if len(x['fields']['components']) != 0 else 'null') for x in data['issues']]
如果您有任何疑问,请告诉我。
推荐阅读
- powershell - 解析信息并仅返回匹配的信息
- vue.js - 排除 json 文件捆绑在 Vue 中
- python - 从地址 IMAP Python 中获取所有未见消息的正文
- spring-boot - 在weblogic中配置spring-boot应用部署的端口
- python-3.x - 我应该如何使用 mode.predict_generator 来评估混淆矩阵中的模型性能?
- r - 按超过 2 列的因子折叠数据框
- java - 如何在不移动按钮的情况下在 javafx 中添加文本框
- c++ - “架构 x86_64 的未定义符号”
- javascript - 函数在 Promise.all(reduce(acc, item) => {})) 函数中丢失上下文
- python - 为什么所有双下划线变量都在类方法中被破坏