python - 如何使用 python 删除字典对象?
问题描述
我有一本试图删除特定对象的字典。
{
"authorizationQualifier": "00",
"testIndicator": " ",
"functionalGroups": [
{
"functionalIdentifierCode": "SC",
"applicationSenderCode": "6088385400",
"applicationReceiverCode": "3147392555",
"transactions": [
{
"name": null,
"transactionSetIdentifierCode": "832",
"transactionSetControlNumber": "000000001",
"implementationConventionReference": null,
"segments": [
{
"BCT": {
"BCT01": "PS",
"BCT03": "2",
"id": "BCT"
}
}
]
}
]
}
]
}
我正在尝试删除对象的“段”列表,同时保留功能组和事务列表。
我试过了,
ediconverted = open("converted.json", "w")
with open('832.json','r') as jsonfile:
json_content = json.load(jsonfile)
for element in json_content:
element.pop('segments', None)
with open('converted.json', 'w') as data_file:
json_content = json.dump(json_content, data_file)
解决方案
对于这个特定的结构,我们将其命名为 d,以下将起作用:
del d["functionalGroups"][0]["transactions"][0]["segments"]
推荐阅读
- java - 为 Kafka schema.registry.ssl.engine.factory.class 提供什么价值
- angular - 如何在图例单击上添加自定义事件
- javascript - Mochawesome 包含 javascript 代码而不是 typescript
- powershell - Powershell 创建文件夹名称
- android-jetpack-compose - Jetpack Compose 中的 NoClassDefFoundError
- git - 为什么远程访问 Heroku Git 时会出错?
- python - 使用 SimpleTransformers 与使用 BERT 的 Transformers 库获得更高的准确度
- python - 使用具有日期时间维度的 xarray DataArray 中的数据着色器进行绘图
- javascript - HTML5 DragDrop Enter和Leave事件依次调用
- python - 加载图像后,我尝试运行音频文件