python建立和修改yaml檔案
阿新 • • 發佈:2020-12-23
1、建立yaml
import os import yaml desired_caps = { 'train': 'dataTrain/2007_train.txt', 'val': 'dataTrain/2007_val.txt', 'nc': 2, 'names': ['a','b'] } curpath = os.path.dirname(os.path.realpath(__file__)) yamlpath = os.path.join(curpath, "./yamlFile/caps.yaml") # 寫入到yaml檔案 with open(yamlpath, "w", encoding="utf-8") as f: yaml.dump(desired_caps, f)
2、修改yaml檔案
import os import yaml def set_state(state): file_name = "./yamlFile/bottlemldel.yaml" with open(file_name) as f: doc = yaml.safe_load(f) doc['nc'] = state with open(file_name, 'w') as f: yaml.safe_dump(doc, f, default_flow_style=False) set_state(8)