import xmltodict
import json
import os
# 输入和输出文件夹路径
input_folder_path = 'Annotations'
output_folder_path = 'Annotations'
# 遍历输入文件夹中的所有文件
for file in os.scandir(input_folder_path):
if file.name.endswith('.xml'):
# 构造输入和输出文件路径
input_file_path = file.path
output_file_path = os.path.join(output_folder_path, file.name.replace('.xml', '.json'))
# 打开输入文件并将其解析为OrderedDict对象
with open(input_file_path, 'r', encoding='utf-8') as input_file:
xml_data = input_file.read()
data_dict = xmltodict.parse(xml_data)
# 将OrderedDict对象转换为JSON格式并写入输出文件
with open(output_file_path, 'w', encoding='utf-8') as output_file:
json.dump(data_dict, output_file, indent=4, ensure_ascii=False)