• 使用python对比两个json文件的不同并输出


    1. import json
    2. def compare_json(obj1, obj2, path=""):
    3. diff = []
    4. if isinstance(obj1, dict) and isinstance(obj2, dict):
    5. for key in obj1:
    6. if key not in obj2:
    7. diff.append([f"{path}.{key}", obj1[key], "", "yes"])
    8. else:
    9. sub_diff = compare_json(obj1[key], obj2[key], f"{path}.{key}")
    10. if len(sub_diff) > 0:
    11. diff += sub_diff
    12. else:
    13. diff.append([f"{path}.{key}", obj1[key], obj2[key], "no"])
    14. for key in obj2:
    15. if key not in obj1:
    16. diff.append([f"{path}.{key}", "", obj2[key], "yes"])
    17. elif isinstance(obj1, list) and isinstance(obj2, list):
    18. for i in range(min(len(obj1), len(obj2))):
    19. sub_diff = compare_json(obj1[i], obj2[i], f"{path}[{i}]")
    20. if len(sub_diff) > 0:
    21. diff += sub_diff
    22. else:
    23. diff.append([f"{path}[{i}]", obj1[i], obj2[i], "no"])
    24. if len(obj1) > len(obj2):
    25. for i in range(len(obj2), len(obj1)):
    26. diff.append([f"{path}[{i}]", obj1[i], "", "yes"])
    27. elif len(obj1) < len(obj2):
    28. for i in range(len(obj1), len(obj2)):
    29. diff.append([f"{path}[{i}]", "", obj2[i], "yes"])
    30. else:
    31. if obj1 != obj2:
    32. diff.append([path, obj1, obj2, "yes"])
    33. else:
    34. diff.append([path, obj1, obj2, "no"])
    35. return diff
    36. def print_diff(diff_keys):
    37. print("-"*110)
    38. print("{:<60} {:<20} {:<20} {:<10}".format("key", "file1.json", "file2.json", "is_diff"))
    39. print("-"*110)
    40. for key, value1, value2, is_diff in diff_keys:
    41. print("{:<60} {:<20} {:<20} {:<10}".format(key, str(value1), str(value2), is_diff))
    42. print("----------------------------------------------------------------------------------------------------------")
    43. def remove_long_keys(data, max_length, keys_to_remove=None):
    44. if keys_to_remove is None:
    45. keys_to_remove = []
    46. if isinstance(data, dict):
    47. for k, v in list(data.items()):
    48. if isinstance(v, str) and len(v) > max_length:
    49. del data[k]
    50. elif k in keys_to_remove:
    51. del data[k]
    52. else:
    53. remove_long_keys(v, max_length, keys_to_remove)
    54. elif isinstance(data, list):
    55. for item in data:
    56. remove_long_keys(item, max_length, keys_to_remove)
    57. if __name__ == "__main__":
    58. with open('file1.json', 'r') as file1:
    59. json1 = json.load(file1)
    60. remove_long_keys(json1, 20, ['seeds'])
    61. with open('file2.json', 'r') as file2:
    62. json2 = json.load(file2)
    63. remove_long_keys(json2, 20, ['seeds'])
    64. diff_keys = compare_json(json1, json2)
    65. print_diff(diff_keys)

    运行结果:
     

  • 相关阅读:
    刷题11.27
    【软件安装】Centos系统中安装docker容器(华为云HECS云耀服务器)
    【技术干货】宇视IPC音频问题解决步骤
    js 正则校验输入是否为空字符串
    2024年全国VUE考试中心大全!
    Milvus以及Web UI 安装
    事件循环原理
    数据分析全貌
    pytorch_lightning:Validation sanity check: 0%| | 0/2 [00:00<?, ?it/s]
    初识深度学习-吴恩达
  • 原文地址:https://blog.csdn.net/fvafuc/article/details/133773663