我有一个很大的json文件,其中包含有关IP地址传输信息的信息。网络地址中的一个或多个块可以转移到另一实体。我想进一步将转移映射到参与转移的单个实体。
Transfers =[{
"original_block": "87.118.128.0/18",
"transferred_blocks": "87.118.144.0/22, 87.118.164.0/22",
"from": "ITD Network SA",
"to": "Bulgarian Telecommunications Company Plc.",
"date": "16/07/2014",
"transferType": "POLICY"
}, {
"original_block": "89.25.0.0/17",
"transferred_blocks": "89.25.40.0/21, 89.25.52.0/22,
89.25.56.0/21, 89.25.100.0/22, 89.25.124.0/22",
"from": "ITD Network SA",
"to": "Bulgarian Telecommunications Company Plc.",
"date": "16/07/2014",
"transferType": "POLICY"
}, {
"original_block": "94.155.0.0/17",
"transferred_blocks": "94.155.104.0/21",
"from": "ITD Network SA",
"to": "Bulgarian Telecommunications Company Plc.",
"date": "16/07/2014",
"transferType": "POLICY"
}]
with open ('Transfers','r') as t_list:#loads the json file
dlist = json.load(t_list)
for k, v in dlist:
dlist[k] = v("transferred_blocks").split(",")
预期输出如下:
dlist =[{
"original_block": "87.118.128.0/18",
"transferred_blocks": "87.118.164.0/22",
"from": "ITD Network SA",
"to": "Bulgarian Telecommunications Company Plc.",
"date": "16/07/2014",
"transferType": "POLICY"
},{
"original_block": "87.118.128.0/18",
"transferred_blocks": "87.118.144.0/22",
"from": "ITD Network SA",
"to": "Bulgarian Telecommunications Company Plc.",
"date": "16/07/2014",
"transferType": "POLICY"
}, {
"original_block": "89.25.0.0/17",
"transferred_blocks": "89.25.40.0/21",
"from": "ITD Network SA",
"to": "Bulgarian Telecommunications Company Plc.",
"date": "16/07/2014",
"transferType": "POLICY"
}, {
"original_block": "89.25.0.0/17",
"transferred_blocks": "89.25.52.0/22",
"from": "ITD Network SA",
"to": "Bulgarian Telecommunications Company Plc.",
"date": "16/07/2014",
"transferType": "POLICY"
}, {
"original_block": "89.25.0.0/17",
"transferred_blocks": "89.25.56.0/21",
"from": "ITD Network SA",
"to": "Bulgarian Telecommunications Company Plc.",
"date": "16/07/2014",
"transferType": "POLICY"
}, {
"original_block": "89.25.0.0/17",
"transferred_blocks": "89.25.100.0/22",
"from": "ITD Network SA",
"to": "Bulgarian Telecommunications Company Plc.",
"date": "16/07/2014",
"transferType": "POLICY"
}, {
"original_block": "89.25.0.0/17",
"transferred_blocks": "89.25.124.0/22",
"from": "ITD Network SA",
"to": "Bulgarian Telecommunications Company Plc.",
"date": "16/07/2014",
"transferType": "POLICY"
}, {
"original_block": "94.155.0.0/17",
"transferred_blocks": "94.155.104.0/21",
"from": "ITD Network SA",
"to": "Bulgarian Telecommunications Company Plc.",
"date": "16/07/2014",
"transferType": "POLICY"
}]
答案 0 :(得分:2)
只需使用列表推导对dlist中的每个字典进行迭代,然后基于transferred_blocks
拆分comma
下的ip地址列表,最后从原始字典创建一个新字典,并更新ip地址
res = [dict(d, transferred_blocks=ip) for d in dlist for ip in d['transferred_blocks'].split(', ')]
print (json.dumps(res, indent=4))
输出
[
{
"original_block": "87.118.128.0/18",
"transferred_blocks": "87.118.144.0/22",
"from": "ITD Network SA",
"to": "Bulgarian Telecommunications Company Plc.",
"date": "16/07/2014",
"transferType": "POLICY"
},
{
"original_block": "87.118.128.0/18",
"transferred_blocks": "87.118.164.0/22",
"from": "ITD Network SA",
"to": "Bulgarian Telecommunications Company Plc.",
"date": "16/07/2014",
"transferType": "POLICY"
},
{
"original_block": "89.25.0.0/17",
"transferred_blocks": "89.25.40.0/21",
"from": "ITD Network SA",
"to": "Bulgarian Telecommunications Company Plc.",
"date": "16/07/2014",
"transferType": "POLICY"
},
{
"original_block": "89.25.0.0/17",
"transferred_blocks": "89.25.52.0/22",
"from": "ITD Network SA",
"to": "Bulgarian Telecommunications Company Plc.",
"date": "16/07/2014",
"transferType": "POLICY"
},
{
"original_block": "89.25.0.0/17",
"transferred_blocks": "89.25.56.0/21",
"from": "ITD Network SA",
"to": "Bulgarian Telecommunications Company Plc.",
"date": "16/07/2014",
"transferType": "POLICY"
},
{
"original_block": "89.25.0.0/17",
"transferred_blocks": "89.25.100.0/22",
"from": "ITD Network SA",
"to": "Bulgarian Telecommunications Company Plc.",
"date": "16/07/2014",
"transferType": "POLICY"
},
{
"original_block": "89.25.0.0/17",
"transferred_blocks": "89.25.124.0/22",
"from": "ITD Network SA",
"to": "Bulgarian Telecommunications Company Plc.",
"date": "16/07/2014",
"transferType": "POLICY"
},
{
"original_block": "94.155.0.0/17",
"transferred_blocks": "94.155.104.0/21",
"from": "ITD Network SA",
"to": "Bulgarian Telecommunications Company Plc.",
"date": "16/07/2014",
"transferType": "POLICY"
}
]