Files
2025-09-25 15:41:00 +08:00

20 lines
800 B
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 读取路径下所有的yaml文件批量替换指定的字符串
import os
import yaml
import glob
import re
# 指定要替换的字符串和新的字符串
old_string = 'NestedMusicTransformer'
new_string = 'AmadeusModel'
# 指定yaml文件所在的目录
directory = 'Amadeus/symbolic_yamls/nn_params'
# 遍历目录下的所有yaml文件
for filepath in glob.glob(os.path.join(directory, '*.yaml')):
with open(filepath, 'r', encoding='utf-8') as file:
content = file.read()
# 使用正则表达式替换指定的字符串
new_content = re.sub(r'\b' + re.escape(old_string) + r'\b', new_string, content)
# 将修改后的内容写回文件
with open(filepath, 'w', encoding='utf-8') as file:
file.write(new_content)
print(f'Processed file: {filepath}')