config_duplicate_2.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. ##################################################################
  4. #
  5. # Copyright (c) 2024 CICV, Inc. All Rights Reserved
  6. #
  7. ##################################################################
  8. """
  9. @Authors: yangzihao(yangzihao@china-icv.cn)
  10. @Data: 2024/04/01
  11. @Last Modified: 2024/04/01
  12. @Summary: Config json drop duplicate.
  13. """
  14. import json
  15. import os
  16. def read_json_file(file_path):
  17. with open(file_path, 'r', encoding='utf-8') as file:
  18. return json.load(file)
  19. def compare_leaf_nodes(node1, node2):
  20. """比较两个叶节点是否相同,不同则返回None"""
  21. if isinstance(node1, dict) and isinstance(node2, dict):
  22. # 如果两个节点都是字典,递归比较它们的值
  23. return {key: compare_leaf_nodes(node1.get(key), node2.get(key)) for key in node1.keys()}
  24. elif isinstance(node1, list) and isinstance(node2, list):
  25. # 如果两个节点都是列表,递归比较它们的元素
  26. return [compare_leaf_nodes(item1, item2) for item1, item2 in zip(node1, node2)]
  27. elif node1 != node2:
  28. # 如果值不同,返回None
  29. return None
  30. else:
  31. # 如果值相同,返回原值
  32. return node1
  33. def compare_json_files(file_paths):
  34. # 读取第一个JSON文件作为基准
  35. base_json = read_json_file(file_paths[0])
  36. # 初始化结果JSON,这里先复制基准JSON
  37. result_json = base_json.copy()
  38. # 遍历剩余的文件进行比较
  39. for file_path in file_paths[1:]:
  40. # 读取当前文件
  41. current_json = read_json_file(file_path)
  42. # 比较基准JSON和当前JSON的叶节点
  43. temp_result = compare_leaf_nodes(base_json, current_json)
  44. # 更新result_json以反映不同
  45. update_dict_with_none(result_json, temp_result)
  46. return result_json
  47. def update_dict_with_none(base_dict, update_dict):
  48. """递归地更新base_dict,将update_dict中的None值赋给base_dict对应的位置"""
  49. for key, value in update_dict.items():
  50. if isinstance(value, dict):
  51. # 如果value是字典,递归更新
  52. update_dict_with_none(base_dict.get(key, {}), value)
  53. elif value is None:
  54. # 如果value是None,更新base_dict中对应位置的值为None
  55. base_dict[key] = None
  56. elif isinstance(base_dict.get(key), list) and isinstance(value, list):
  57. # 如果base_dict中的值是列表,且update_dict中的值也是列表,则更新列表元素
  58. base_dict[key] = [update_value if update_value is None else base_value
  59. for base_value, update_value in zip(base_dict[key], value)]
  60. def write_json_file(file_path, data):
  61. with open(file_path, 'w', encoding='utf-8') as file:
  62. json.dump(data, file, indent=4, ensure_ascii=False)
  63. def main():
  64. # 假设您的JSON文件都在当前目录下的一个名为'json_files'的文件夹中
  65. json_dir = './jsonFiles'
  66. file_paths = [os.path.join(json_dir, f) for f in os.listdir(json_dir) if f.endswith('.json')]
  67. # 比较这些文件
  68. result_json = compare_json_files(file_paths)
  69. # 将结果写入新的JSON文件
  70. output_file_path = './output-2.json'
  71. write_json_file(output_file_path, result_json)
  72. print(f'Comparison result saved to {output_file_path}')
  73. if __name__ == '__main__':
  74. main()