一、参考链接
https://pyyaml.org/wiki/PyYAMLDocumentation
二、python类型转换为yaml
# -*- coding: utf-8 -*-
# @Time : 2022/1/2 21:53
# @Author : lujunxian
# @File : test_yaml.py
import yaml
class TestYaml():
with open('./data.yaml','w',encoding='utf-8') as f:
request={
'test':{
'url':'test url',
'username':'test01'
},
'uat':{
'url':'uat url'
}
}
yaml.dump(request,f)
生成 data.yaml 如下
test:
url: test url
username: test01
uat:
url: uat url
三、yaml转换为python类型
with open('./data.yaml','r',encoding='utf-8') as f:
print(yaml.safe_load(f))
结果如下
{'test': {'url': 'test url', 'username': 'test01'}, 'uat': {'url': 'uat url'}}
总结
本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注编程网的更多内容!