python修改yaml文件
目录
yaml读写示例
ruamel读取
import yaml
with open('./0709.yaml','r',encoding='utf8') as file:#utf8可识别中文
data=yaml.safe_load(file)
nc=data['nc']
print(nc)
train_list=data['train']
print(train_list)
val_list=data['val']
print(val_list)
# 修改 YAML 文件
data['your_key'] = 'your_value'
# 写入 YAML 文件
with open('your_file.yaml', 'w') as file:
yaml.safe_dump(data, file)
from ruamel.yaml import YAML
yaml = YAML()
# 读取 YAML 文件
with open('your_file.yaml', 'r') as file:
data = yaml.load(file)
# 修改 YAML 文件
data['your_key'] = 'your_value'
# 写入 YAML 文件
with open('your_file.yaml', 'w') as file:
yaml.dump(data, file)