yaml文件的【创建】【修改】

创建yaml文件

import os
import yaml
content = {
                'batch_size': 64,
                'dataset_dir': 'data/PEMS-BAY',
                'test_batch_size': 64,
                'val_batch_size': 64,
                'horizon': 12,
                ......
                }

# prepath = os.path.dirname(os.path.realpath('__file__'))
# path = os.path.join(prepath, "./xxx.yaml")
path = os.path.join("./xxx.yaml")

# 写入到yaml文件
with open(path, "w", encoding="utf-8") as f:
    yaml.dump(content, f)

修改yaml文件

import os
import yaml
def alter_yaml(key,value):
    file_name = "./xxx.yaml"
    with open(file_name) as f:
        doc = yaml.safe_load(f)
    doc[key] = value
    with open(file_name, 'w') as f:
        yaml.safe_dump(doc, f, default_flow_style=False)

你可能感兴趣的:(Python,深度学习,深度学习,人工智能)