Python deepdiff库怎么使用

其他教程   发布日期:2025年03月22日   浏览次数:128

这篇文章主要介绍“Python deepdiff库怎么使用”,在日常操作中,相信很多人在Python deepdiff库怎么使用问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Python deepdiff库怎么使用”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

deepdiff库

安装

  1. pip install deepdiff

说明

deepdiff模块常用来校验两个对象是否一致,并找出其中差异之处,它提供了:

deepdiff模块常用来校验两个对象是否一致,并找出其中差异之处,它提供了:

  • DeepDiff:比较两个对象,对象可以是字段、字符串等可迭代的对象

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

  • DeepHash:根据对象的内容进行哈希处理

DeepDiff

  • 作用:比较两个对象,对象可以是字段、字符串等可迭代的对象

说明:

  • type_changes:类型改变的key

  • values_changed:值发生变化的key

  • dictionary_item_added:字典key添加

  • dictionary_item_removed:字段key删除

对比json

  1. # -*-coding:utf-8一*-
  2. # @Time:2023/4/16
  3. # @Author: DH
  4. from deepdiff import DeepDiff
  5. # json校验
  6. json_one = {
  7. 'code': 0,
  8. "message": "失败",
  9. 'data': {
  10. 'id': 1
  11. }
  12. }
  13. json_two = {
  14. 'code': 1,
  15. "message": "成功",
  16. 'data': {
  17. 'id': 1
  18. }
  19. }
  20. print(DeepDiff(json_one, json_two))
  21. # 输出
  22. """
  23. {'values_changed': {"root['code']": {'new_value': 1, 'old_value': 0}, "root['message']": {'new_value': '成功', 'old_value': '失败'}}}
  24. root['code'] : 改变值的路径
  25. new_value : 新值
  26. old_value :原值
  27. """

列表校验

cutoff_distance_for_pairs: (1 >= float > 0,默认值=0.3);通常结合ignore_order=true使用,用于结果中展示差异的深度。值越高,则结果中展示的差异深度越高。

  1. from deepdiff import DeepDiff
  2. t1 = [[[1.0, 666], 888]]
  3. t2 = [[[20.0, 666], 999]]
  4. print(DeepDiff(t1, t2, ignore_order=True, cutoff_distance_for_pairs=0.5))
  5. print(DeepDiff(t1, t2, ignore_order=True)) # 默认为0.3
  6. print(DeepDiff(t1, t2, ignore_order=True, cutoff_distance_for_pairs=0.2))
  7. """
  8. {'values_changed': {'root[0][0]': {'new_value': [20.0, 666], 'old_value': [1.0, 666]}, 'root[0][1]': {'new_value': 999, 'old_value': 888}}}
  9. {'values_changed': {'root[0]': {'new_value': [[20.0, 666], 999], 'old_value': [[1.0, 666], 888]}}}
  10. {'values_changed': {'root[0]': {'new_value': [[20.0, 666], 999], 'old_value': [[1.0, 666], 888]}}}
  11. """

忽略字符串类型

ignore_string_type_changes :忽略校验字符串类型,默认为False

  1. print(DeepDiff(b'hello', 'hello', ignore_string_type_changes=True))
  2. print(DeepDiff(b'hello', 'hello'))
  3. """
  4. 输出:
  5. {}
  6. {'type_changes': {'root': {'old_type': <class 'bytes'>, 'new_type': <class 'str'>, 'old_value': b'hello', 'new_value': 'hello'}}}
  7. """

忽略大小写

ignore_string_case:忽略大小写,默认为False

  1. from deepdiff import DeepDiff
  2. print(DeepDiff(t1='Hello', t2='heLLO'))
  3. print(DeepDiff(t1='Hello', t2='heLLO', ignore_string_case=True))
  4. """
  5. 输出:
  6. {'values_changed': {'root': {'new_value': 'heLLO', 'old_value': 'Hello'}}}
  7. {}
  8. """

DeepSearch

作用:在对象中搜索其他对象 查找字典key/value

  1. from deepdiff import DeepSearch
  2. json_three = {
  3. 'code': 1,
  4. "message": "成功",
  5. 'data': {
  6. 'id': 1
  7. }
  8. }
  9. # 查找key
  10. print(DeepSearch(json_three, "code"))
  11. print(DeepSearch(json_three, "name"))
  12. # 查找value
  13. print(DeepSearch(json_three, 1))
  14. """
  15. 输出:
  16. {'matched_paths': ["root['code']"]}
  17. {}
  18. {'matched_values': ["root['code']", "root['data']['id']"]}
  19. """
  20. # 正则 use_regexp
  21. obj = ["long somewhere", "string", 0, "somewhere great!"]
  22. # 使用正则表达式
  23. item = "some*"
  24. ds = DeepSearch(obj, item, use_regexp=True)
  25. print(ds)
  26. # 强校验 strict_checking 默认True
  27. item = '0'
  28. ds = DeepSearch(obj, item, strict_checking=False)
  29. # ds = DeepSearch(obj, item) # 默认True
  30. print(ds)
  31. # 大小写敏感 case_sensitive 默认 False 敏感
  32. item = 'someWhere'
  33. ds = DeepSearch(obj, item, case_sensitive=True)
  34. print(ds)

DeepHash

作用:根据对象的内容进行哈希处理

  1. from deepdiff import DeepHash
  2. # 对对象进行hash
  3. json_four = {
  4. 'code': 1,
  5. "message": "成功",
  6. 'data': {
  7. 'id': 1
  8. }
  9. }
  10. print(DeepHash(json_four))

extract

extract : 根据路径查询值

  1. from deepdiff import extract
  2. # 根据路径查询值
  3. obj = {1: [{'2': 666}, 3], 2: [4, 5]}
  4. path = "root[1][0]['2']"
  5. value = extract(obj, path)
  6. print(value)
  7. """
  8. 输出:
  9. 666
  10. """

grep

搜索

  1. from deepdiff import grep
  2. obj = ["long somewhere", "string", 0, "somewhere great!"]
  3. item = "somewhere"
  4. ds = obj | grep(item)
  5. print(ds)
  6. # use_regexp 为True 表示支持正则
  7. obj = ["something here", {"long": "somewhere", "someone": 2, 0: 0, "somewhere": "around"}]
  8. ds = obj | grep("some.*", use_regexp=True)
  9. print(ds)
  10. # 根据值查询路径
  11. obj = {1: [{'2': 'b'}, 3], 2: [4, 5, 5]}
  12. result = obj | grep(5)
  13. print(result)
  14. """
  15. 输出:
  16. {'matched_values': ['root[2][1]', 'root[2][2]']}
  17. """

以上就是Python deepdiff库怎么使用的详细内容,更多关于Python deepdiff库怎么使用的资料请关注九品源码其它相关文章!