有没有办法拆分字典键的值?

2024-06-01 03:45:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个很大的json文件,其中包含关于ip地址传输信息的信息。网络地址中的一个或多个块可以传输到另一个实体。我想进一步映射到参与转移的单个实体it。你知道吗

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"
    }]

Tags: tofromdatesanetworkblockcompanyplc
1条回答
网友
1楼 · 发布于 2024-06-01 03:45:03

只需使用列表理解来迭代dlist中的每个dict,然后基于comma将ip地址列表拆分到transferred_blocks下,最后使用更新的ip addr从原始dict创建一个新dict

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"
    }
]

相关问题 更多 >