python - 如何在 Python 中处理嵌套字典中的嵌套列表?
问题描述
我有以下类型的数据,其中 dict 具有嵌套的 dicts 以及一些嵌套列表
输入
{
"properties": {
"property": [
{
"propertyId": "508276",
"propertyName": "State On Campus Norman",
"budgets": {
"budget": [
{
"id": "32133",
"budgetName": "Norman 2020 Budget",
"budgetStatusTypeId": "1",
"isDefault": "1",
"fiscalStartDate": "01/01/2020",
"fiscalEndDate": "12/31/2020",
"fiscalYear": "2020",
"budgetTotal": "-88245.12",
"glAccounts": {
"glAccount": [
{
"id": 159475,
"accountName": "Residential Rental Revenue",
"accountNumber": "40000",
"glTotals": {
"glTotal": [
{
"amount": 3880956
}
]
},
"budgetMonths": {
"budgetMonth": [
{
"month": "01/01/2020",
"amount": 321888
},
{
"month": "02/01/2020",
"amount": 321888
}]
}
},{
"id": 242922,
"accountName": "Parking Revenue",
"accountNumber": "40100",
"glTotals": {
"glTotal": [
{
"amount": 14760
}
]
},
"budgetMonths": {
"budgetMonth": [
{
"month": "01/01/2020",
"amount": 1230
},
{
"month": "02/01/2020",
"amount": 1230
}
]
}
}
]
}
},
{
"id": "12299",
"budgetName": "Norman 2020 Budget",
"budgetStatusTypeId": "1",
"isDefault": "1",
"fiscalStartDate": "01/01/2020",
"fiscalEndDate": "12/31/2020",
"fiscalYear": "2020",
"budgetTotal": "-88245.12",
"glAccounts": {
"glAccount": [
{
"id": 159475,
"accountName": "Residential Rental Revenue",
"accountNumber": "40000",
"glTotals": {
"glTotal": [
{
"amount": 3880956
}
]
},
"budgetMonths": {
"budgetMonth": [
{
"month": "01/01/2020",
"amount": 321888
},
{
"month": "02/01/2020",
"amount": 321888
}]
}
},{
"id": 242922,
"accountName": "Parking Revenue",
"accountNumber": "40100",
"glTotals": {
"glTotal": [
{
"amount": 14760
}
]
},
"budgetMonths": {
"budgetMonth": [
{
"month": "01/01/2020",
"amount": 1230
},
{
"month": "02/01/2020",
"amount": 1230
}
]
}
}
]
}
}
]
}
}
]
}
}
我必须将上述数据转换为以下格式。我尝试了很多方法,但无法成功。也尝试过使用 flattern lib,但它只对初始嵌套进行了扁平化。我需要找出动态的方式。
输出
{
"properties": [
{
"propertyId": "508276",
"propertyName": "State On Campus Norman"
}
],
"budgets": [
{
"propertyId": "508276",
"id": "32133",
"budgetName": "Norman 2020 Budget",
"budgetStatusTypeId": "1",
"isDefault": "1",
"fiscalStartDate": "01/01/2020",
"fiscalEndDate": "12/31/2020",
"fiscalYear": "2020",
"budgetTotal": "-88245.12"
},
{
"propertyId": "508276",
"id": "12299",
"budgetName": "Norman 2020 Budget",
"budgetStatusTypeId": "1",
"isDefault": "1",
"fiscalStartDate": "01/01/2020",
"fiscalEndDate": "12/31/2020",
"fiscalYear": "2020",
"budgetTotal": "-88245.12"
}
],
"glAccounts": [
{
"id": 159475,
"budgets_id": "32133",
"accountName": "Residential Rental Revenue",
"accountNumber": "40000"
},
{
"id": 242922,
"budgets_id": "32133",
"accountName": "Parking Revenue",
"accountNumber": "40100"
},
{
"id": 212343,
"budgets_id": "12299",
"accountName": "Residential Rental Revenue",
"accountNumber": "34343"
},
{
"id": 455454,
"budgets_id": "12299",
"accountName": "Parking Revenue",
"accountNumber": "32323"
}
],
"glTotals": [{some data list...}],
"budgetMonths": [{some data list...}]
}
我用来解决这个问题的 Python 代码
from flatten_dict import flatten
#'inp' is the input which I have mentioned above
flat = flatten(inp, reducer=lambda k1, k2: k2 if k1 is None else k1 + '.' + k2)
print(flat)
#Output from this code
{
"properties.property": [
{
"propertyId": "508276",
"propertyName": "State On Campus Norman",
"budgets": {
"budget": [.......
请帮助使用python解决这个问题。
解决方案
这个问题的解决方案
import json
inp = {}
cout = {}
def flatrn_dict(inp, name, pk_name):
cop_in = inp.copy()
pkn, pkv = identify_id(cop_in)
for k, v in cop_in.items():
if type(v) == dict:
if pkv is not None:
v.update({pkn: pkv})
flatrn_dict(v, k, name)
inp.pop(k)
elif type(v) == list:
for vx in v:
if pkn is not None:
pkn = "{0}_id".format(pk_name)
vx.update({pkn: pkv})
flatrn_dict(vx, k, name)
arr = cout.get(name)
if arr is None:
cout.update({name: v})
else:
arr.extend(v)
cout.update({name: arr})
inp.pop(k)
def identify_id(ele):
for k,v in ele.items():
if 'id' in k.lower():
return k, v
return None, None
if __name__ == "__main__":
with open("test1.json", "r") as f:
inp = json.load(f)
flatrn_dict(inp, "prop", "")
with open("test_out.json", "w") as fw:
fw.write(json.dumps(cout))
推荐阅读
- c# - 使用 Katalon 在运行时调用不同的环境配置
- java - 能够根据选择的选项选择正确的文本文件
- reactjs - 在 React 中添加组件
- php - Codeigniter 在哪里使用 AND
- python-3.x - 在 Python 3 中高效地生成字符串
- memory-management - alloc_calls 和 free_calls 是什么意思?
- php - 在 Wordpress 页脚中垂直对齐自定义创建的菜单
- amazon-web-services - Echo Spot 有时需要几分钟才能开始播放视频
- spring-boot - 如何对我的独立(非 Web 服务)应用程序进行 Spring 化?
- python - Python ping 脚本