1.安装包,pip install deepdiff
2.导入包:
from deepdiff import DeepDiff
3.
dict1 = {"aaa":"AAA","bbb":"BBB"}
dict2 = {"c":"AAA","bbb":"BBB"}
print(DeepDiff(dict1,dict2))
输入:{‘dictionary_item_added‘: [root[‘c‘]], ‘dictionary_item_removed‘: [root[‘aaa‘]]}
对于dict1来说 dict2新增了 key c
对于dict2来说 缺失 dict2得 key aaa
4.
dict7 = {"c":"AAA","bbb":"BBB1",‘eee‘:[{"DDD":"1233","fff":"2344"}]}
dict6 = {"c":"AAA","bbb":"BBB2",‘eee‘:[{"DDD":"1234","fff":"2345"}]}
print(DeepDiff(dict7,dict6))
输出:{‘values_changed‘: {"root[‘bbb‘]": {‘new_value‘: ‘BBB2‘, ‘old_value‘: ‘BBB1‘}, "root[‘eee‘][0][‘DDD‘]": {‘new_value‘: ‘1234‘, ‘old_value‘: ‘1233‘}, "root[‘eee‘][0][‘fff‘]": {‘new_value‘: ‘2345‘, ‘old_value‘: ‘2344‘}}}}}}
values值改变了
5.
dict4 = {"c":"AAA","bbb":"BBB",‘ee‘:[{"DDD":"1234","fff":"2345"}]}
dict5 = {"c":"AAA","bbb":"BBB",‘eee‘:[{"DDD":"1234","fff":"2345"}]}
print(DeepDiff(dict4,dict5))
输出:
{‘dictionary_item_added‘: [root[‘eee‘]], ‘dictionary_item_removed‘: [root[‘ee‘]]}
key值错误
详见代码: