Python中高效的json對比庫deepdiff詳解
工作中我們經(jīng)常要兩段代碼的區(qū)別,或者需要查看接口返回的字段與預(yù)期是否一致,如何快速定位出兩者的差異?除了一些對比的工具比如Beyond Compare、WinMerge等,或者命令工具diff(在linux環(huán)境下使用),其實Python中也提供了很多實現(xiàn)對比的庫,比如deepdiff和difflib,這兩個的區(qū)別是deepdiff顯示的對比效果比較簡潔,但是可以設(shè)置忽略的字段,difflib顯示的對比結(jié)果可以是html的,比較詳細(xì)。今天我們就學(xué)習(xí)一下快速實現(xiàn)代碼和文件對比的庫–deepdiff。
deepdiff是什么
deepdiff
模塊常用來校驗兩個對象是否一致,包含3個常用類,DeepDiff,DeepSearch和DeepHash,其中DeepDiff最常用,可以對字典,可迭代對象,字符串等進(jìn)行對比,使用遞歸地查找所有差異。當(dāng)然,也可以可以用來校驗多種文件內(nèi)容的差異,如txt、json、圖片等…
https://github.com/seperman/deepdiff
deepdiff安裝
pip install deepdiff
如果實際請求結(jié)果和預(yù)期值的json數(shù)據(jù)都一致,那么會返回{}空字典,否則會返回對比差異的結(jié)果,接口測試中我們也可以根據(jù)這個特點進(jìn)行斷言。
導(dǎo)入
>>> from deepdiff import DeepDiff # For Deep Difference of 2 objects >>> from deepdiff import grep, DeepSearch # For finding if item exists in an object >>> from deepdiff import DeepHash # For hashing objects based on their contents
如果對比結(jié)果不同,將會給出下面對應(yīng)的返回:
- 1、type_changes:類型改變的key
- 2、values_changed:值發(fā)生變化的key
- 3、dictionary_item_added:字典key添加
- 4、dictionary_item_removed:字段key刪除
案例1、對比txt文件
from deepdiff import DeepDiff """ a.txt的內(nèi)容是: abc b.txt的內(nèi)容是: abcd """ f1, f2 = open('a.txt', 'r', encoding='utf-8').read(), open('b.txt', 'r', encoding='utf-8').read() print(DeepDiff(f1, f2)) # 輸出結(jié)果,內(nèi)容值發(fā)生變化 {'values_changed': {'root': {'new_value': 'abcd', 'old_value': 'abc'}}}
案例2、對比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)) # 輸出結(jié)果,id移除,total值發(fā)生改變 #{'dictionary_item_removed': [root['data']['id']], 'values_changed': {"root['data']['total']": {'new_value': 29, 'old_value': 28}}}
DeepDiff在單元測試中的應(yīng)用
import unittest import requests from deepdiff import DeepDiff class MyCase(unittest.TestCase): 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' } } ? def setUp(self): self.response = requests.get('http://www.httpbin.org/json').json() print(self.response) ? def test_case_01(self): print(DeepDiff(self.response, self.expect)) ? def test_case_02(self): print(DeepDiff(self.response['slideshow']['author'], 'Yours Truly1')) ? if __name__ == '__main__': unittest.main()
測試用例1實際返回和預(yù)期結(jié)果json完全一樣,輸出結(jié)果為:{},即兩者沒有差異。
測試用例2斷言返回author與期望值,值發(fā)生變化。
其實,在實際接口斷言中,可能需要校驗的字段順序不一樣,又或者有一些字段值不需要,為了解決這類問題,Deepdiff也提供了相信的參數(shù),只需要在比較的時候加入,傳入對應(yīng)參數(shù)即可。
ignore order
(忽略排序)ignore string case
(忽略大小寫)exclude_paths
排除指定的字段
print(DeepDiff(self.response, self.expect,view='tree',ignore_order=True,ignore_string_case=True,exclude_paths={"root['slideshow']['date']"}))
更多的參數(shù)使用可以,進(jìn)入源碼中查看:
更多關(guān)于DeepDiff的使用可以查看下面的文檔:
https://zepworks.com/deepdiff/5.8.2/diff.html
https://zepworks.com/deepdiff/5.8.2/
https://zepworks.com/tags/deepdiff/
到此這篇關(guān)于Python中高效的json對比庫deepdiff詳解的文章就介紹到這了,更多相關(guān)Python json對比庫deepdiff內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
用Python實現(xiàn)BP神經(jīng)網(wǎng)絡(luò)(附代碼)
這篇文章主要介紹了用Python實現(xiàn)BP神經(jīng)網(wǎng)絡(luò)(附代碼),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07使用python把json文件轉(zhuǎn)換為csv文件
這篇文章主要介紹了使用python把json文件轉(zhuǎn)換為csv文件,幫助大家更好的利用python處理數(shù)據(jù),感興趣的朋友可以了解下2021-03-03Tensorflow讀取并輸出已保存模型的權(quán)重數(shù)值方式
今天小編就為大家分享一篇Tensorflow讀取并輸出已保存模型的權(quán)重數(shù)值方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看不看2020-01-01在CentOS 7中使用Python 3執(zhí)行系統(tǒng)命令的詳細(xì)教程
使用os.system()這個方法簡單直接,但它不返回命令的輸出,只返回命令的退出狀態(tài),如果你只需要知道命令是否成功執(zhí)行,這個方法就足夠了,這篇文章主要介紹了在CentOS 7中使用Python 3執(zhí)行系統(tǒng)命令的詳細(xì)教程,需要的朋友可以參考下2024-02-02python使用for...else跳出雙層嵌套循環(huán)的方法實例
這篇文章主要給大家介紹了關(guān)于python使用for...else跳出雙層嵌套循環(huán)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用python具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05