40 lines
1.1 KiB
Python
40 lines
1.1 KiB
Python
|
import os
|
||
|
import json
|
||
|
|
||
|
# 设置你的目标文件夹路径
|
||
|
folder_path = "../../data/part5/精标"
|
||
|
|
||
|
# 定义替换映射
|
||
|
replace_dict = {
|
||
|
"引擎盖": 2,
|
||
|
"前保险杠": 1,
|
||
|
"前挡风玻璃": 4,
|
||
|
"背景": 0,
|
||
|
"A柱": 3
|
||
|
}
|
||
|
|
||
|
def replace_labels_in_json(file_path):
|
||
|
with open(file_path, 'r', encoding='utf-8') as f:
|
||
|
data = json.load(f)
|
||
|
|
||
|
def replace(obj):
|
||
|
if isinstance(obj, dict):
|
||
|
return {k: replace(v) for k, v in obj.items()}
|
||
|
elif isinstance(obj, list):
|
||
|
return [replace(item) for item in obj]
|
||
|
elif isinstance(obj, str) and obj in replace_dict:
|
||
|
return replace_dict[obj]
|
||
|
else:
|
||
|
return obj
|
||
|
|
||
|
new_data = replace(data)
|
||
|
|
||
|
with open(file_path, 'w', encoding='utf-8') as f:
|
||
|
json.dump(new_data, f, ensure_ascii=False, indent=2)
|
||
|
|
||
|
if __name__ == "__main__":
|
||
|
for filename in os.listdir(folder_path):
|
||
|
if filename.endswith('.json'):
|
||
|
file_path = os.path.join(folder_path, filename)
|
||
|
replace_labels_in_json(file_path)
|
||
|
print("替换完成!")
|