pip install deepdiff
deepdiff模块常用来校验两个对象是否一致,并找出其中差异之处,它提供了:
说明:
- type_changes:类型改变的key
- values_changed:值发生变化的key
- dictionary_item_added:字典key添加
- dictionary_item_removed:字段key删除
# -*-coding:utf-8一*-
# @Time:2023/4/16
# @Author: DH
from deepdiff import DeepDiff
# json校验
json_one = {
'code': 0,
"message": "失败",
'data': {
'id': 1
}
}
json_two = {
'code': 1,
"message": "成功",
'data': {
'id': 1
}
}
print(DeepDiff(json_one, json_two))
# 输出
"""
{'values_changed': {"root['code']": {'new_value': 1, 'old_value': 0}, "root['message']": {'new_value': '成功', 'old_value': '失败'}}}
root['code'] : 改变值的路径
new_value : 新值
old_value :原值
"""
from deepdiff import DeepDiff
t1 = [[[1.0, 666], 888]]
t2 = [[[20.0, 666], 999]]
print(DeepDiff(t1, t2, ignore_order=True, cutoff_distance_for_pairs=0.5))
print(DeepDiff(t1, t2, ignore_order=True)) # 默认为0.3
print(DeepDiff(t1, t2, ignore_order=True, cutoff_distance_for_pairs=0.2))
"""
{'values_changed': {'root[0][0]': {'new_value': [20.0, 666], 'old_value': [1.0, 666]}, 'root[0][1]': {'new_value': 999, 'old_value': 888}}}
{'values_changed': {'root[0]': {'new_value': [[20.0, 666], 999], 'old_value': [[1.0, 666], 888]}}}
{'values_changed': {'root[0]': {'new_value': [[20.0, 666], 999], 'old_value': [[1.0, 666], 888]}}}
"""
print(DeepDiff(b'hello', 'hello', ignore_string_type_changes=True))
print(DeepDiff(b'hello', 'hello'))
"""
输出:
{}
{'type_changes': {'root': {'old_type': , 'new_type': , 'old_value': b'hello', 'new_value': 'hello'}}}
"""
from deepdiff import DeepDiff
print(DeepDiff(t1='Hello', t2='heLLO'))
print(DeepDiff(t1='Hello', t2='heLLO', ignore_string_case=True))
"""
输出:
{'values_changed': {'root': {'new_value': 'heLLO', 'old_value': 'Hello'}}}
{}
"""
from deepdiff import DeepSearch
json_three = {
'code': 1,
"message": "成功",
'data': {
'id': 1
}
}
# 查找key
print(DeepSearch(json_three, "code"))
print(DeepSearch(json_three, "name"))
# 查找value
print(DeepSearch(json_three, 1))
"""
输出:
{'matched_paths': ["root['code']"]}
{}
{'matched_values': ["root['code']", "root['data']['id']"]}
"""
# 正则 use_regexp
obj = ["long somewhere", "string", 0, "somewhere great!"]
# 使用正则表达式
item = "some*"
ds = DeepSearch(obj, item, use_regexp=True)
print(ds)
# 强校验 strict_checking 默认True
item = '0'
ds = DeepSearch(obj, item, strict_checking=False)
# ds = DeepSearch(obj, item) # 默认True
print(ds)
# 大小写敏感 case_sensitive 默认 False 敏感
item = 'someWhere'
ds = DeepSearch(obj, item, case_sensitive=True)
print(ds)
from deepdiff import DeepHash
# 对对象进行hash
json_four = {
'code': 1,
"message": "成功",
'data': {
'id': 1
}
}
print(DeepHash(json_four))
from deepdiff import extract
# 根据路径查询值
obj = {1: [{'2': 666}, 3], 2: [4, 5]}
path = "root[1][0]['2']"
value = extract(obj, path)
print(value)
"""
输出:
666
"""
from deepdiff import grep
obj = ["long somewhere", "string", 0, "somewhere great!"]
item = "somewhere"
ds = obj | grep(item)
print(ds)
# use_regexp 为True 表示支持正则
obj = ["something here", {"long": "somewhere", "someone": 2, 0: 0, "somewhere": "around"}]
ds = obj | grep("some.*", use_regexp=True)
print(ds)
# 根据值查询路径
obj = {1: [{'2': 'b'}, 3], 2: [4, 5, 5]}
result = obj | grep(5)
print(result)
"""
输出:
{'matched_values': ['root[2][1]', 'root[2][2]']}
"""