12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- """
- @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):
-
- base_json = read_json_file(file_paths[0])
-
- result_json = {}
-
- for key in base_json:
-
- result_value = base_json[key]
-
- for file_path in file_paths[1:]:
-
- current_json = read_json_file(file_path)
-
- if key not in current_json or current_json[key] != result_value:
-
- result_value = None
- break
-
- 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_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)
-
- 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()
|