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

Python 有没有办法拆分字典键的值?,python,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":

我有一个很大的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)
对于数据列表中的k,v:
数据列表[k]=v(“传输的块”)。拆分(“,”)

预期输出如下所示:

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

只需使用列表理解来迭代dlist中的每个dict,然后根据
逗号将ip地址列表拆分到
传输的\u块下
,最后使用更新的ip地址从原始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"
    }
]

请告诉我们您的努力和路障。我已经做了调整,它给出了错误“太多的值无法打开(预期2)”工作良好。非常感谢。