提取两个python嵌套字典之间的差异

时间:2016-05-31 02:32:54

标签: python dictionary

我试图提取两个嵌套字典之间的差异。

我已经安装了DeepDiff,这看起来是我想要实现的最佳选择。我试图确定如何更改输出,以便它提供字典值而不是我无法索引的集合。有没有更好的方法来改变输出(而不是将输出转换回字典)

from __future__ import print_function
from deepdiff import DeepDiff
knownAPs = {'WLC1': {'10.1.1.1': {'72.6': ['AP22', 'city'], '55.1': ['AP102', 'office']}}, 'WLC2': {'10.1.1.2': {}}}
discoveredAPs = {'WLC1': {'10.1.1.1': {}}, 'WLC2': {'10.1.1.2': {}}}
ddiff = DeepDiff(knownAPs, discoveredAPs)
print(ddiff)
if 'dic_item_added' in ddiff.keys():
    print('Item added to known: ' + str((ddiff['dic_item_added'])))
if 'dic_item_removed' in ddiff.keys():
    DisAssociatedAPs = (list(list(ddiff['dic_item_removed'])))
    for i in DisAssociatedAPs:
        keypath = (str(i).strip('root'))
        print("AP's removed from known database: " + str(knownAPs + keypath))
if 'values_changed' in ddiff.keys():
    print('Item changed: ' + str((ddiff['values_changed'])))

错误

{'dic_item_removed': set(["root['WLC1']['10.1.1.1']['72.6']", "root['WLC1']['10.1.1.1']['55.1']"])}
TypeError: unsupported operand type(s) for +: 'dict' and 'str'

首选输出

{'dic_item_removed': set(["root['WLC1']['10.1.1.1']['72.6']", "root['WLC1']['10.1.1.1']['55.1']"])}    
AP's removed from known database: ['AP22', 'city']
AP's removed from known database: ['AP102', 'office']

0 个答案:

没有答案