|
|
import os |
|
|
import json |
|
|
|
|
|
def replace_underscores_in_task_type(data): |
|
|
"""处理JSON数据,替换task_type中的下划线""" |
|
|
for item in data: |
|
|
if isinstance(item, dict) and 'task_type' in item: |
|
|
original = item['task_type'] |
|
|
if isinstance(original, str): |
|
|
item['task_type'] = original.replace('_', '-') |
|
|
return data |
|
|
|
|
|
def process_json_file(file_path): |
|
|
"""处理单个JSON文件""" |
|
|
try: |
|
|
with open(file_path, 'r+', encoding='utf-8') as f: |
|
|
|
|
|
data = json.load(f) |
|
|
if not isinstance(data, list): |
|
|
print(f" 已跳过非数组文件:{file_path}") |
|
|
return |
|
|
|
|
|
|
|
|
modified_data = replace_underscores_in_task_type(data) |
|
|
f.seek(0) |
|
|
json.dump(modified_data, f, indent=4, ensure_ascii=False) |
|
|
f.truncate() |
|
|
|
|
|
print(f" 成功处理:{os.path.basename(file_path)}") |
|
|
|
|
|
except Exception as e: |
|
|
print(f" 处理失败 [{os.path.basename(file_path)}]:{str(e)}") |
|
|
|
|
|
def process_directory(target_dir): |
|
|
"""处理目录下的JSON文件(不包含子目录)""" |
|
|
for file in os.listdir(target_dir): |
|
|
if file.lower().endswith('.json'): |
|
|
file_path = os.path.join(target_dir, file) |
|
|
if os.path.isfile(file_path): |
|
|
process_json_file(file_path) |
|
|
|
|
|
if __name__ == '__main__': |
|
|
input_directory = "/mnt/data/users/zys/proj/vlm_reasoning/utils/json" |
|
|
|
|
|
if os.path.isdir(input_directory): |
|
|
print(f" 正在处理目录:{input_directory}") |
|
|
process_directory(input_directory) |
|
|
print(" 处理完成") |
|
|
else: |
|
|
print(f" 目录不存在:{input_directory}") |