12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- ##################################################################
- #
- # Copyright (c) 2024 CICV, Inc. All Rights Reserved
- #
- ##################################################################
- """
- @Authors: yangzihao(yangzihao@china-icv.cn)
- @Data: 2024/04/01
- @Last Modified: 2024/04/01
- @Summary: Config json drop duplicate.
- """
- import json
- import os
- def read_json_file(file_path):
- with open(file_path, 'r', encoding='utf-8') as file:
- return json.load(file)
- def compare_leaf_nodes(node1, node2):
- """比较两个叶节点是否相同,不同则返回None"""
- if isinstance(node1, dict) and isinstance(node2, dict):
- # 如果两个节点都是字典,递归比较它们的值
- return {key: compare_leaf_nodes(node1.get(key), node2.get(key)) for key in node1}
- elif node1 != node2:
- # 如果值不同,返回None
- return None
- else:
- # 如果值相同,返回原值
- return node1
- def compare_json_files(file_paths):
- # 读取第一个JSON文件作为基准
- base_json = read_json_file(file_paths[0])
- # 创建一个字典来存储比较结果
- result_json = {}
- # 遍历剩余的文件进行比较
- for file_path in file_paths[1:]:
- # 读取当前文件
- current_json = read_json_file(file_path)
- # 比较基准JSON和当前JSON的叶节点
- result_json = compare_leaf_nodes(base_json, current_json)
- # 一旦发现不同,就跳出循环,因为我们已经得到了需要的结果
- if None in result_json.values():
- break
- return result_json
- def write_json_file(file_path, data):
- with open(file_path, 'w', encoding='utf-8') as file:
- json.dump(data, file, indent=4, ensure_ascii=False)
- def main():
- # 假设您的JSON文件都在当前目录下的一个名为'json_files'的文件夹中
- json_dir = './jsonFiles'
- file_paths = [os.path.join(json_dir, f) for f in os.listdir(json_dir) if f.endswith('.json')]
- # 比较这些文件
- result_json = compare_json_files(file_paths)
- # 将结果写入新的JSON文件
- output_file_path = './output1.json'
- write_json_file(output_file_path, result_json)
- print(f'Comparison result saved to {output_file_path}')
- if __name__ == '__main__':
- main()
|