deepdiff常用方法

pip安装deepdiff

pip install deepdiff

比对txt文档

from deepdiff import DeepDiff

"""
a.txt的内容是: abc
b.txt的内容是: abcd
"""
f1, f2 = open('a.txt', 'r', encoding='utf-8').read(), open('b.txt', 'r', encoding='utf-8').read()
print(DeepDiff(f1, f2))

result:

{'values_changed': {'root': {'new_value': 'abcd', 'old_value': 'abc'}}}

比对Json

from deepdiff import DeepDiff

json1 = {
    'code': 0,
    "message": "成功",
    "data": {
        "total": 28,
        "id": 123
    }}
json2 = {
    'code': 0,
    "message": "成功",
    "data": {
        "total": 29,
    }
}
print(DeepDiff(json1, json2))

result:

{'dictionary_item_removed': [root['data']['id']], 'values_changed': {"root['data']['total']": {'new_value': 29, 'old_value': 28}}}

比对Dict

import requests
from deepdiff import DeepDiff


expect = {
    'slideshow': {
        'author': 'Yours Truly',
        'date': 'date of publication',
        'slides': [{
            'title': 'Wake up to WonderWidgets!',
            'type': 'all'
        }, {
            'items': ['Why <em>WonderWidgets</em> are great', 'Who< em > buys < / em > WonderWidgets'],
            'title': 'Overview',
            'type': 'all'
        }],
        'title': 'Sample Slide Show'
    }
}



# 返回字典格式报文
response = requests.get('http://www.httpbin.org/json').json()


print("Case1:")
print(f'实际:{response}')
print(f'预期:{expect}')
print(f'比对结果:{DeepDiff(response, expect)}')

print("Case2:")
print(f"实际:{response['slideshow']['author']}")
print(f"预期:Yours Truly1")
print(f"比对结果:{DeepDiff(response['slideshow']['author'], 'Yours Truly1')}")

print("Case3:")
print(f"实际:{response['slideshow']['author']}")
print(f"预期:Yours Truly")
print(f"比对结果:{DeepDiff(response['slideshow']['author'], 'Yours Truly')}")

Result:

Case1:
实际:{'slideshow': {'author': 'Yours Truly', 'date': 'date of publication', 'slides': [{'title': 'Wake up to WonderWidgets!', 'type': 'all'}, {'items': ['Why <em>WonderWidgets</em> are great', 'Who <em>buys</em> WonderWidgets'], 'title': 'Overview', 'type': 'all'}], 'title': 'Sample Slide Show'}}
预期:{'slideshow': {'author': 'Yours Truly', 'date': 'date of publication', 'slides': [{'title': 'Wake up to WonderWidgets!', 'type': 'all'}, {'items': ['Why <em>WonderWidgets</em> are great', 'Who< em > buys < / em > WonderWidgets'], 'title': 'Overview', 'type': 'all'}], 'title': 'Sample Slide Show'}}
比对结果:{'values_changed': {"root['slideshow']['slides'][1]['items'][1]": {'new_value': 'Who< em > buys < / em > WonderWidgets', 'old_value': 'Who <em>buys</em> WonderWidgets'}}}
Case2:
实际:Yours Truly
预期:Yours Truly1
比对结果:{'values_changed': {'root': {'new_value': 'Yours Truly1', 'old_value': 'Yours Truly'}}}
Case3:
实际:Yours Truly
预期:Yours Truly
比对结果:{}

cutoff_distance_for_pairs

from deepdiff import DeepDiff
t1 = [[[1.0]]]
t2 = [[[20.0]]]
print(DeepDiff(t1, t2, ignore_order=True, cutoff_distance_for_pairs=0.3))
print(DeepDiff(t1, t2, ignore_order=True, cutoff_distance_for_pairs=0.2))
print(DeepDiff(t1, t2, ignore_order=True, cutoff_distance_for_pairs=0.1))

Result:

{'values_changed': {'root[0][0][0]': {'new_value': 20.0, 'old_value': 1.0}}}
{'values_changed': {'root[0][0]': {'new_value': [20.0], 'old_value': [1.0]}}}
{'values_changed': {'root[0]': {'new_value': [[20.0]], 'old_value': [[1.0]]}}}

ignore_string_case忽略大小写

import requests
from deepdiff import DeepDiff


expect = {
    'slideshow': {
        'title': 'sample Slide Show',
        # 'title1': 'Sample Slide Show',
        'author': 'Yours Truly',
        'date': 'date of publication',
        'slides': [{
            'title': 'Wake up to WonderWidgets!',
            'type': 'all'
        }, {
            'items': ['Why <em>WonderWidgets</em> are great', 'Who <em>buys</em> WonderWidgets'],
            'title': 'Overview',
            'type': 'all'
        }],
    }
}



# 返回字典格式报文
response = requests.get('http://www.httpbin.org/json').json()

print("Case1:忽略大小写")
print(f'实际:{response}')
print(f'预期:{expect}')
print(f'比对结果:{DeepDiff(response, expect)}')
print(f'比对结果:{DeepDiff(response, expect, ignore_string_case=True)}')

Result:

Case1:忽略大小写
实际:{'slideshow': {'author': 'Yours Truly', 'date': 'date of publication', 'slides': [{'title': 'Wake up to WonderWidgets!', 'type': 'all'}, {'items': ['Why <em>WonderWidgets</em> are great', 'Who <em>buys</em> WonderWidgets'], 'title': 'Overview', 'type': 'all'}], 'title': 'Sample Slide Show'}}
预期:{'slideshow': {'title': 'sample Slide Show', 'author': 'Yours Truly', 'date': 'date of publication', 'slides': [{'title': 'Wake up to WonderWidgets!', 'type': 'all'}, {'items': ['Why <em>WonderWidgets</em> are great', 'Who <em>buys</em> WonderWidgets'], 'title': 'Overview', 'type': 'all'}]}}
比对结果:{'values_changed': {"root['slideshow']['title']": {'new_value': 'sample Slide Show', 'old_value': 'Sample Slide Show'}}}
比对结果:{}

exclude_paths

import requests
from deepdiff import DeepDiff


expect = {
    'slideshow': {
        'title': 'sample Slide Show',
        # 'title1': 'Sample Slide Show',
        'author': 'Yours Truly',
        'date': 'date of publication',
        'slides': [{
            'title': 'Wake up to WonderWidgets!',
            'type': 'all'
        }, {
            'items': ['Why <em>WonderWidgets</em> are great', 'Who <em>buys</em> WonderWidgets'],
            'title': 'Overview',
            'type': 'all'
        }],
    }
}



# 返回字典格式报文
response = requests.get('http://www.httpbin.org/json').json()
print("Case2:排除指定字段")
print(f'实际:{response}')
print(f'预期:{expect}')
print(f'比对结果:{DeepDiff(response, expect)}')
print(DeepDiff(response, expect, exclude_paths={"root['slideshow']['title']"}))

Result:

Case2:排除指定字段
实际:{'slideshow': {'author': 'Yours Truly', 'date': 'date of publication', 'slides': [{'title': 'Wake up to WonderWidgets!', 'type': 'all'}, {'items': ['Why <em>WonderWidgets</em> are great', 'Who <em>buys</em> WonderWidgets'], 'title': 'Overview', 'type': 'all'}], 'title': 'Sample Slide Show'}}
预期:{'slideshow': {'title': 'sample Slide Show', 'author': 'Yours Truly', 'date': 'date of publication', 'slides': [{'title': 'Wake up to WonderWidgets!', 'type': 'all'}, {'items': ['Why <em>WonderWidgets</em> are great', 'Who <em>buys</em> WonderWidgets'], 'title': 'Overview', 'type': 'all'}]}}
比对结果:{'values_changed': {"root['slideshow']['title']": {'new_value': 'sample Slide Show', 'old_value': 'Sample Slide Show'}}}
{}

ignore_order

import requests
from deepdiff import DeepDiff


expect = {
    'slideshow': {
        'title': 'Sample Slide Show',
        # 'title1': 'Sample Slide Show',
        'author': 'Yours Truly',
        'date': 'date of publication',
        'slides': [{
            'title': 'Wake up to WonderWidgets!',
            'type': 'all'
        }, {
            'items': ['Why <em>WonderWidgets</em> are great', 'Who <em>buys</em> WonderWidgets'],
            'title': 'Overview',
            'type': 'all'
        }],
    }
}

# 返回字典格式报文
response = requests.get('http://www.httpbin.org/json').json()
print("Case3:忽略字段排序")
print(f'实际:{response}')
print(f'预期:{expect}')
print(f'比对结果:{DeepDiff(response, expect, ignore_order=True)}')

Result:

Case3:忽略字段排序
实际:{'slideshow': {'author': 'Yours Truly', 'date': 'date of publication', 'slides': [{'title': 'Wake up to WonderWidgets!', 'type': 'all'}, {'items': ['Why <em>WonderWidgets</em> are great', 'Who <em>buys</em> WonderWidgets'], 'title': 'Overview', 'type': 'all'}], 'title': 'Sample Slide Show'}}
预期:{'slideshow': {'title': 'Sample Slide Show', 'author': 'Yours Truly', 'date': 'date of publication', 'slides': [{'title': 'Wake up to WonderWidgets!', 'type': 'all'}, {'items': ['Why <em>WonderWidgets</em> are great', 'Who <em>buys</em> WonderWidgets'], 'title': 'Overview', 'type': 'all'}]}}
比对结果:{}

有新增字段

import requests
from deepdiff import DeepDiff


expect = {
    'slideshow': {
        'title': 'Sample Slide Show',
        'title1': 'Sample Slide Show',
        'author': 'Yours Truly',
        'date': 'date of publication',
        'slides': [{
            'title': 'Wake up to WonderWidgets!',
            'type': 'all'
        }, {
            'items': ['Why <em>WonderWidgets</em> are great', 'Who <em>buys</em> WonderWidgets'],
            'title': 'Overview',
            'type': 'all'
        }],
    }
}


# 返回字典格式报文
response = requests.get('http://www.httpbin.org/json').json()
print("Case4:有新增字段")
print(f'实际:{response}')
print(f'预期:{expect}')
print(f'比对结果:{DeepDiff(response, expect)}')

Result:

Case4:有新增字段
实际:{'slideshow': {'author': 'Yours Truly', 'date': 'date of publication', 'slides': [{'title': 'Wake up to WonderWidgets!', 'type': 'all'}, {'items': ['Why <em>WonderWidgets</em> are great', 'Who <em>buys</em> WonderWidgets'], 'title': 'Overview', 'type': 'all'}], 'title': 'Sample Slide Show'}}
预期:{'slideshow': {'title': 'Sample Slide Show', 'title1': 'Sample Slide Show', 'author': 'Yours Truly', 'date': 'date of publication', 'slides': [{'title': 'Wake up to WonderWidgets!', 'type': 'all'}, {'items': ['Why <em>WonderWidgets</em> are great', 'Who <em>buys</em> WonderWidgets'], 'title': 'Overview', 'type': 'all'}]}}
比对结果:{'dictionary_item_added': [root['slideshow']['title1']]}

有丢失字段

import requests
from deepdiff import DeepDiff


expect = {
    'slideshow': {
        # 'title': 'Sample Slide Show',
        # 'title1': 'Sample Slide Show',
        'author': 'Yours Truly',
        'date': 'date of publication',
        'slides': [{
            'title': 'Wake up to WonderWidgets!',
            'type': 'all'
        }, {
            'items': ['Why <em>WonderWidgets</em> are great', 'Who <em>buys</em> WonderWidgets'],
            'title': 'Overview',
            'type': 'all'
        }],
    }
}



# 返回字典格式报文
response = requests.get('http://www.httpbin.org/json').json()

print("Case5:有丢失字段")
print(f'实际:{response}')
print(f'预期:{expect}')
print(f'比对结果:{DeepDiff(response, expect)}')

Result:

Case5:有丢失字段
实际:{'slideshow': {'author': 'Yours Truly', 'date': 'date of publication', 'slides': [{'title': 'Wake up to WonderWidgets!', 'type': 'all'}, {'items': ['Why <em>WonderWidgets</em> are great', 'Who <em>buys</em> WonderWidgets'], 'title': 'Overview', 'type': 'all'}], 'title': 'Sample Slide Show'}}
预期:{'slideshow': {'author': 'Yours Truly', 'date': 'date of publication', 'slides': [{'title': 'Wake up to WonderWidgets!', 'type': 'all'}, {'items': ['Why <em>WonderWidgets</em> are great', 'Who <em>buys</em> WonderWidgets'], 'title': 'Overview', 'type': 'all'}]}}
比对结果:{'dictionary_item_removed': [root['slideshow']['title']]}

ignore_numeric_type_changes

from decimal import Decimal
from deepdiff import DeepDiff
t1 = Decimal('10.01')
t2 = 10.01
print(DeepDiff(t1, t2))
print(DeepDiff(t1, t2, ignore_numeric_type_changes=True))

Result:

{'type_changes': {'root': {'old_type': <class 'decimal.Decimal'>, 'new_type': <class 'float'>, 'old_value': Decimal('10.01'), 'new_value': 10.01}}}
{}

ignore_string_type_changes

from deepdiff import DeepDiff
s1 = b'hello'
s2 = 'hello'
print(DeepDiff(s1, s2))
print(DeepDiff(s1, s2, ignore_string_type_changes=True))

Result:

{'type_changes': {'root': {'old_type': <class 'bytes'>, 'new_type': <class 'str'>, 'old_value': b'hello', 'new_value': 'hello'}}}
{}

view

from deepdiff import DeepDiff
t1= {"name": "zhangsan", "pro": {"sh": "shandong", "city": ["zibo", "weifang"]}}
t2 = {"name": "lisi", "pro": {"sh": "shandong", "town": ["taian", "weifang"]}}
ddiff = DeepDiff(t1, t2, view='tree')
print(ddiff)
# 默认为text
ddiff = DeepDiff(t1, t2, view='text')
print(ddiff)

Result:

{'dictionary_item_removed': [<root['pro']['city'] t1:['zibo', 'we...], t2:not present>], 'dictionary_item_added': [<root['pro']['town'] t1:not present, t2:['taian', 'w...]>], 'values_changed': [<root['name'] t1:'zhangsan', t2:'lisi'>]}
{'dictionary_item_added': [root['pro']['town']], 'dictionary_item_removed': [root['pro']['city']], 'values_changed': {"root['name']": {'new_value': 'lisi', 'old_value': 'zhangsan'}}}

Pretty

示例1:

from deepdiff import DeepDiff
t1= {"name": "yanan", "pro": {"sh": "shandong", "city": ["zibo", "weifang"]}}
t2 = {"name": "changsha", "pro": {"sh": "shandong", "town": ["taian", "weifang"]}}
ddiff = DeepDiff(t1, t2, view='tree').pretty()
print(ddiff)
# 默认为text
ddiff = DeepDiff(t1, t2, view='text').pretty()
print(ddiff)

Result:

Item root['pro']['town'] added to dictionary.
Item root['pro']['city'] removed from dictionary.
Value of root['name'] changed from "yanan" to "changsha".
Item root['pro']['town'] added to dictionary.
Item root['pro']['city'] removed from dictionary.
Value of root['name'] changed from "yanan" to "changsha".

示例2:

t1 = {
    'Author': '测试同学',
    'wechat': 'ZZ666'
}
t2 = {
    'Author': '测试同学',
    'wechat': 'ZZ666',
    'Blog': 'https://www.hctestedu.com/'
}
t3 = {
    'Author': '测试同学',
    'wechat': 'ZZ777'
}
t4 = {
    'Author': '测试同学',
    'wechat': 777
}
t5 = [{
    'Author': '测试同学',
    'wechat': 'ZZ666'
}]
# Key值不同
print(DeepDiff(t1, t3).pretty())
# Key新增
print(DeepDiff(t1, t2).pretty())
# Key减少
print(DeepDiff(t2, t1).pretty())

# Key值类型改变
print(DeepDiff(t1, t4).pretty())
# 结构不同
print(DeepDiff(t1, t5).pretty())
# Key值相同
result = DeepDiff(t1, t1).pretty()
print(DeepDiff(t1, t1).pretty())
assert "" == result

Result:

Value of root['wechat'] changed from "ZZ666" to "ZZ777".
Item root['Blog'] added to dictionary.
Item root['Blog'] removed from dictionary.
Type of root['wechat'] changed from str to int and value changed from "ZZ666" to 777.
Type of root changed from dict to list and value changed from {'Author': '测试同学', 'wechat': 'ZZ666'} to [{'Author': '测试同学', 'wechat': 'ZZ666'}].

DeepSearch

from deepdiff import DeepSearch

obj = ["long somewhere", "string", 0, "somewhere great!"]
# 使用正则表达式
item = "some*"
ds = DeepSearch(obj, item, use_regexp=True)
print(f'1:{ds}')
# 大小写敏感
item = 'someWhere'
ds = DeepSearch(obj, item, case_sensitive=True)
print(f'2:{ds}')
item = 'some'
ds = DeepSearch(obj, item, case_sensitive=True)
print(f'3:{ds}')
# 强校验
item = 0
ds = DeepSearch(obj, item, strict_checking=True)
print(f'4:{ds}')
item = "0"
ds = DeepSearch(obj, item, strict_checking=True)
print(f'5:{ds}')


Result:

1:{'matched_values': ['root[0]', 'root[3]']}
2:{}
3:{'matched_values': ['root[0]', 'root[3]']}
4:{'matched_values': ['root[2]']}
5:{}

Grep

from deepdiff import grep
obj = ["long somewhere", "string", 0, "somewhere great!"]
item = "somewhere"
ds = obj | grep(item)
print(ds)

Result:

{'matched_values': ['root[0]', 'root[3]']}

extract

from deepdiff import extract
obj = {"a": [{'2': 'b'}, 3], "b": [4, 5]}
# root+键名+list下标+键名
path = "root[a][0]['2']"
print(extract(obj, path))

Result:

b
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Summer@123

不积跬步无以至千里,感谢支持!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值