循环浏览字典并跟踪关键深度

时间:2016-04-29 15:56:40

标签: python json python-2.7 loops dictionary

我希望比较两个词典。目标是找出之前存在的键,但不是之后(丢失),以及匹配键中更改的值(已更改)

我有一个伪工作脚本,但问题是它没有显示更改的键/值对所属的所有键。

import json
from pprint import pprint

def new_keys(d1, d2, result={}):
    for key in d1.keys():
        if not d2.has_key(key):
            result[key] = d1[key]
        else:
            if isinstance(d1[key], dict) and isinstance(d2[key], dict):
                new_keys(d1[key], d2[key], result)
            else:
                if d1[key] != d2[key]:
                    result[key] = d2[key]

    return result

with open("before.json") as data_file:
    before = json.loads(data_file.read())

with open("after.json") as data_file:
    after = json.loads(data_file.read())

pprint(new_keys(before,after))

目前的结果是:

{u'1.1.1.1/32': {u'directlyConnected': True,
                 u'hardwareProgrammed': True,
                 u'kernelProgrammed': True,
                 u'routeAction': u'drop',
                 u'routeType': u'static',
                 u'vias': []},

我想要的一个示例结果是:(它是一个不同的路由0.0.0.0/0而不是1.1.1.1/32)但请注意它嵌套在vrfs,default,routes keys中。

{u'vrfs': {u'default': {u'allRoutesProgrammedHardware': True,
                        u'allRoutesProgrammedKernel': True,
                        u'defaultRouteState': u'reachable',
                        u'routes': {u'0.0.0.0/0': {u'directlyConnected': False,
                                                   u'hardwareProgrammed': True,
                                                   u'kernelProgrammed': True,
                                                   u'metric': 0,
                                                   u'preference': 1,
                                                   u'routeAction': u'forward',
                                                   u'routeType': u'static',
                                                   u'vias': [{u'interface': u'Management1',
                                                              u'nexthopAddr': u'10.31.32.1'}]},

我怀疑我需要以某种方式跟踪密钥,因为我更深入,但我在编码时遇到了困难。

非常感谢任何帮助!

编辑:示例JSON

{
    "vrfs": {
        "default": {
            "routes": {
                "2.2.2.2/32": {
                    "kernelProgrammed": true, 
                    "directlyConnected": true, 
                    "routeAction": "forward", 
                    "vias": [
                        {
                            "interface": "Ethernet4"
                        }
                    ], 
                    "hardwareProgrammed": true, 
                    "routeType": "static"
                }
            }, 
            "allRoutesProgrammedKernel": true, 
            "routingDisabled": false, 
            "allRoutesProgrammedHardware": true, 
            "defaultRouteState": "reachable"
        }
    }
}

之后 -

{
    "vrfs": {
        "default": {
            "routes": {
                "2.2.2.2/32": {
                    "kernelProgrammed": true, 
                    "directlyConnected": true, 
                    "routeAction": "forward", 
                    "vias": [
                        {
                            "interface": "Ethernet9"
                        }
                    ], 
                    "hardwareProgrammed": true, 
                    "routeType": "static"
                }
            }, 
            "allRoutesProgrammedKernel": true, 
            "routingDisabled": false, 
            "allRoutesProgrammedHardware": true, 
            "defaultRouteState": "reachable"
        }
    }
}

界面值已更改。

1 个答案:

答案 0 :(得分:0)

这是基于我上面评论的可能解决方案。还有一些pythonic的改进。使用iteritems()key not in dict

import json
from pprint import pprint

def new_keys(d1, d2, result={}):
    for key, d1_val in d1.iteritems():
        if key not in d2:
            result[key] = d1_val
        else:
            if isinstance(d1_val, dict) and isinstance(d2[key], dict):
                # Don't pass result.
                new_result = {}
                new_keys(d1[key], d2[key], new_result)
                # If new_result is not empty
                if new_result:
                   result[key] = new_result
            else:
                if d1_val != d2[key]:
                   result[key] = d1_val

    return result

with open("before.json") as data_file:
    before = json.loads(data_file.read())

with open("after.json") as data_file:
    after = json.loads(data_file.read())
pprint(new_keys(before,after))

使用json之前和之后的样本,这就是我得到的

{u'vrfs': {u'default': {u'routes': {u'2.2.2.2/32': {u'vias': [{u'interface': u'Ethernet4'}]}}}}}