yaml文件的读写是真的快,也很简单。代码如下:
from ruamel.yaml import YAML
import os
标签:fp,配置文件,encoding,yaml,path,data From: https://www.cnblogs.com/archerhzy/p/17123850.html
# 读取yaml配置文件
def read_yaml(yaml_path):
yaml = YAML(typ='unsafe')
try:
with open(yaml_path, encoding="utf-8") as fp:
yaml_data = yaml.load(fp)
fp.close()
return yaml_data
except:
return None
# 写入yaml配置文件
def write_yaml(yaml_path, data, encoding="utf-8"):
yaml = YAML(typ="rt")
with open(yaml_path, encoding=encoding, mode="w") as fp:
yaml.dump(data, stream=fp)
fp.close()