12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- #!/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_json_files(file_paths):
- # 读取第一个JSON文件作为基准
- base_json = read_json_file(file_paths[0])
- # 创建一个字典来存储比较结果
- result_json = {}
- # 遍历基准JSON的所有key
- for key in base_json:
- # 初始化所有文件的该key的值为基准值
- result_value = base_json[key]
- # 遍历剩余的文件进行比较
- for file_path in file_paths[1:]:
- # 读取当前文件
- current_json = read_json_file(file_path)
- # 检查key是否存在,并且值是否相同
- if key not in current_json or current_json[key] != result_value:
- # 如果不同,则将该key的值设为null
- result_value = None
- break
- # 将最终值添加到结果JSON中
- result_json[key] = result_value
- return result_json
- def write_json_file(file_path, data):
- with open(file_path, 'w', encoding='utf-8') as file:
- json.dump(data, file, ensure_ascii=False, indent=4)
- 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 = './output.json'
- write_json_file(output_file_path, result_json)
- print(f'Comparison result saved to {output_file_path}')
- if __name__ == '__main__':
- main()
|