yaml方法封装

下载pyyaml模块

pip  pip --default-timeout=100 install pyyaml -i [http://pypi.douban.com/simple/](http://pypi.douban.com/simple/) --trusted-host [pypi.douban.com](http://pypi.douban.com/)

yaml封装

import yaml
from config.config import CaseDataPath  # 导入config中yaml文件路径类


class YamlHandler():


    def __init__(self,file_path):
        self.file_path = file_path

    def yaml_read(self,encoding="utf-8"):
        with open(self.file_path,'r',encoding=encoding) as f:
            return yaml.full_load(f)

    def yaml_write(self,data,encoding="utf-8"):
        with open(self.file_path,"w",encoding=encoding) as f:
            return yaml.dump(data,stream=f,allow_unicode=True)

if __name__ == '__main__':
    # 实例化yaml_handler
    yaml_handler =  YamlHandler(CaseDataPath.test_path)
    # 读取yaml文件
    test = yaml_handler.yaml_read()
    # 修改yaml文件
    test["password"] = 111111
    yaml_handler.yaml_write(test)
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。