Python對比校驗(yàn)神器deepdiff庫使用詳解
工作中我們經(jīng)常要兩段代碼的區(qū)別,或者需要查看接口返回的字段與預(yù)期是否一致,如何快速定位出兩者的差異?除了一些對比的工具比如Beyond Compare、WinMerge等,或者命令工具diff(在linux環(huán)境下使用),其實(shí)Python中也提供了很多實(shí)現(xiàn)對比的庫,比如deepdiff和difflib,這兩個(gè)的區(qū)別是deepdiff顯示的對比效果比較簡潔,但是可以設(shè)置忽略的字段,difflib顯示的對比結(jié)果可以是html的,比較詳細(xì)。今天我們就學(xué)習(xí)一下快速實(shí)現(xiàn)代碼和文件對比的庫–deepdiff。
前言
在接口自動(dòng)化中會遇到想要得出兩次響應(yīng)體(json值)差異,本篇來學(xué)習(xí)的deepdiff庫可以解決這問題
deepdiff庫
安裝
pip install deepdiff
說明
deepdiff模塊常用來校驗(yàn)兩個(gè)對象是否一致,并找出其中差異之處,它提供了:
deepdiff模塊常用來校驗(yàn)兩個(gè)對象是否一致,并找出其中差異之處,它提供了:
- DeepDiff:比較兩個(gè)對象,對象可以是字段、字符串等可迭代的對象
- DeepSearch:在對象中搜索其他對象
- DeepHash:根據(jù)對象的內(nèi)容進(jìn)行哈希處理
DeepDiff
- 作用:比較兩個(gè)對象,對象可以是字段、字符串等可迭代的對象
說明:
- type_changes:類型改變的key
- values_changed:值發(fā)生變化的key
- dictionary_item_added:字典key添加
- dictionary_item_removed:字段key刪除
對比json
# -*-coding:utf-8一*- # @Time:2023/4/16 # @Author: DH from deepdiff import DeepDiff # json校驗(yàn) 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 :原值 """
列表校驗(yàn)
cutoff_distance_for_pairs: (1 >= float > 0,默認(rèn)值=0.3);通常結(jié)合ignore_order=true使用,用于結(jié)果中展示差異的深度。值越高,則結(jié)果中展示的差異深度越高。
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)) # 默認(rèn)為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]}}} """
忽略字符串類型
ignore_string_type_changes :忽略校驗(yàn)字符串類型,默認(rèn)為False
print(DeepDiff(b'hello', 'hello', ignore_string_type_changes=True)) print(DeepDiff(b'hello', 'hello')) """ 輸出: {} {'type_changes': {'root': {'old_type': <class 'bytes'>, 'new_type': <class 'str'>, 'old_value': b'hello', 'new_value': 'hello'}}} """
忽略大小寫
ignore_string_case:忽略大小寫,默認(rèn)為False
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'}}} {} """
DeepSearch
作用:在對象中搜索其他對象 查找字典key/value
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!"] # 使用正則表達(dá)式 item = "some*" ds = DeepSearch(obj, item, use_regexp=True) print(ds) # 強(qiáng)校驗(yàn) strict_checking 默認(rèn)True item = '0' ds = DeepSearch(obj, item, strict_checking=False) # ds = DeepSearch(obj, item) # 默認(rèn)True print(ds) # 大小寫敏感 case_sensitive 默認(rèn) False 敏感 item = 'someWhere' ds = DeepSearch(obj, item, case_sensitive=True) print(ds)
DeepHash
作用:根據(jù)對象的內(nèi)容進(jìn)行哈希處理
from deepdiff import DeepHash # 對對象進(jìn)行hash json_four = { 'code': 1, "message": "成功", 'data': { 'id': 1 } } print(DeepHash(json_four))
extract
extract : 根據(jù)路徑查詢值
from deepdiff import extract # 根據(jù)路徑查詢值 obj = {1: [{'2': 666}, 3], 2: [4, 5]} path = "root[1][0]['2']" value = extract(obj, path) print(value) """ 輸出: 666 """
grep
搜索
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) # 根據(jù)值查詢路徑 obj = {1: [{'2': 'b'}, 3], 2: [4, 5, 5]} result = obj | grep(5) print(result) """ 輸出: {'matched_values': ['root[2][1]', 'root[2][2]']} """
到此這篇關(guān)于Python-對比校驗(yàn)神器-deepdiff庫的文章就介紹到這了,更多相關(guān)Python對比庫deepdiff內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python實(shí)現(xiàn)搜索文本文件內(nèi)容腳本
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)搜索文本文件內(nèi)容的腳本,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06基于Pytorch實(shí)現(xiàn)分類器的示例詳解
這篇文章主要為大家詳細(xì)介紹了如何基于Pytorch實(shí)現(xiàn)兩個(gè)分類器:?softmax分類器和感知機(jī)分類器,文中的示例代碼講解詳細(xì),需要的可以參考一下2023-04-04Python中不同進(jìn)制間的轉(zhuǎn)換實(shí)現(xiàn)
在計(jì)算機(jī)科學(xué)中,需要進(jìn)行不同進(jìn)制之間的轉(zhuǎn)換,本文主要介紹了Python中不同進(jìn)制間的轉(zhuǎn)換,具有一定的參考價(jià)值,感興趣的可以了解一下2023-10-10Python+Pygame實(shí)戰(zhàn)之英文版猜字游戲的實(shí)現(xiàn)
這篇文章主要為大家介紹了如何利用Python中的Pygame模塊實(shí)現(xiàn)英文版猜單詞游戲,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)Python游戲開發(fā)有一定幫助,需要的可以參考一下2022-08-08Python實(shí)現(xiàn)簡繁體轉(zhuǎn)換
很多時(shí)候簡繁體轉(zhuǎn)換,掌握了簡體與繁體的轉(zhuǎn)換,往往能夠事半功倍,本文主要介紹了Python實(shí)現(xiàn)簡繁體轉(zhuǎn)換,感興趣的可以了解一下2021-06-06python中time tzset()函數(shù)實(shí)例用法
在本篇文章里小編給大家整理的是一篇關(guān)于python中time tzset()函數(shù)實(shí)例用法內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。2021-02-02