美文网首页
python deepdiff 总结

python deepdiff 总结

作者: 黑哈哈二大爷 | 来源:发表于2023-08-24 21:12 被阅读0次

from deepdiff import DeepDiff

t1 = {"name": "changsha", "pro": {"sh": "shandong", "hhh": 33,
"town": [{"sh": "shandong", "hhh": 33, "town": ["weifang1", "taian1","jajaa"]}, "weifang",
"iiii"]}, "time": 2}
t2 = {"name": "changsha", "pro": {"sh": "shandong", "hhh": 33,
"town": [{"sh": "shandong1", "hhh": 33, "town": ["weifang", "taian"]}, "taian"]},
"time": 3}
t3 = {"name": "yanan", "pro": {"sh": "shandong", "city": ["zibo", "weifang"]}, 'time': 1}

ddiff1 = DeepDiff(t1, t2)
print(ddiff1)
denoiseJsonKey=ddiff1['values_changed'].keys()

需要降噪的字段

print((denoiseJsonKey))

降噪之后两文件对比

ddiff2 = DeepDiff(t1, t2, view="tree", ignore_string_case=True, exclude_paths=denoiseJsonKey)
print(ddiff2)

非降噪 两文件对比

ddiff3 = DeepDiff(t1, t2, view="tree", ignore_string_case=True)
print(ddiff3)

按照降噪字段去除两文件的降噪值

for k,v in enumerate([t1,t2]):
for i in denoiseJsonKey:
if k==0:
i=i.replace('root','t1')
else:
i=i.replace('root','t2')
if i[-2] ==''':
i=i
else:
inum=i.rindex('[')
i=i[0:inum]
continue
try:
exec(f"del {i}")
except Exception as e:
continue
print(t1)
print(t2)

from deepdiff import DeepDiff
from deepdiff import grep
from deepdiff import DeepSearch
from deepdiff import extract
"""
DeepDiff grep 、DeepSearch可以根据正则表达式来搜索和提取差异。它使用正则表达式来匹配输入对象中的路径和值,并在找到匹配的路径时返回相应的差异。

extract:Get the item from obj based on path.

exclude_types是DeepDiff中的一个选项,用于指定要排除比较的数据类型:exclude_types={str,int}
ignore_order=True: 可以设置忽略指定情况的顺序【1,3,2,4】 【1,2,3,4】 忽略后这俩 就是没有区别了
ignore_string_type_changes=True : 忽略字符串类型的差异
ignore_numeric_type_changes=True:忽略数字类型的差异
ignore_string_case=True:忽略字母大小写的差异
verbose_level=2 : 对比差异的大小 一般为2 就可以
"""
dict1 = {"one": 1, "two": 2, "three": 7,"four":"two"}
dict2 = {"one": 1, "two": 4, "three": "6"}
list1 = [1,2,3]
list2 = [1,2,3,3]
res = DeepDiff(dict1,dict2)
res1 = DeepDiff(dict1,dict2,exclude_types={str,int})
res2 = DeepDiff(dict1,dict2, view="tree",ignore_order=True,report_repetition=True,verbose_level=2,get_deep_distance=True)
print(res.to_dict())
print(res.to_json())
print(res.pretty())
print(dict1 |grep ("two"))
print(dict1 |grep (2))
print(DeepSearch(dict1,'two',verbose_level=2))
print(extract(dict1,"root['two']"))
print(dict1 |grep ("two")['matched_values'][0])
print(extract(dict1,dict1 |grep ("two")['matched_values'][0]))

相关文章

网友评论

      本文标题:python deepdiff 总结

      本文链接:https://www.haomeiwen.com/subject/emkvmdtx.html