python创建和修改yaml文件

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)

你可能感兴趣的:(python创建和修改yaml文件)