下载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)