Python读写yaml文档的代码片段

发布时间 2023-05-22 11:03:01作者: RonyJay
import os
import yaml


# 写入
def write_yaml(data):
    with open(os.getcwd() + "/extract.yaml", encoding="utf-8", mode='a+') as f:
        yaml.dump(data, stream=f, allow_unicode=True)


# 读取
def read_yaml(key):
    with open(os.getcwd() + "/extract.yaml", encoding="utf-8", mode='r') as f:
        data = yaml.load(f, Loader=yaml.FullLoader)
        return data[key]


# 清空
def clear_yaml():
    with open(os.getcwd() + "/extract.yaml", encoding="utf-8", mode='w') as f:
        f.truncate()