溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Python deepdiff庫怎么使用

發(fā)布時間:2023-04-20 11:26:31 來源:億速云 閱讀:93 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要介紹“Python deepdiff庫怎么使用”,在日常操作中,相信很多人在Python deepdiff庫怎么使用問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Python deepdiff庫怎么使用”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!

deepdiff庫

安裝

pip install deepdiff

說明

deepdiff模塊常用來校驗兩個對象是否一致,并找出其中差異之處,它提供了:

deepdiff模塊常用來校驗兩個對象是否一致,并找出其中差異之處,它提供了:

  • DeepDiff:比較兩個對象,對象可以是字段、字符串等可迭代的對象

  • DeepSearch:在對象中搜索其他對象

  • DeepHash:根據(jù)對象的內(nèi)容進行哈希處理

DeepDiff

  • 作用:比較兩個對象,對象可以是字段、字符串等可迭代的對象

說明:

  • 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校驗
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 :原值
"""

列表校驗

cutoff_distance_for_pairs: (1 >= float > 0,默認值=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)) # 默認為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 :忽略校驗字符串類型,默認為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:忽略大小寫,默認為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!"]
# 使用正則表達式
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)

DeepHash

作用:根據(jù)對象的內(nèi)容進行哈希處理

from deepdiff import DeepHash

# 對對象進行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 deepdiff庫怎么使用”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI