当前位置: 首页> 房产> 政策 > python学习14:如何读取yaml文件?

python学习14:如何读取yaml文件?

时间:2025/7/15 21:24:59来源:https://blog.csdn.net/m0_53780713/article/details/141896531 浏览次数:0次
yaml是专门用来写配置文件的语言,类似于json格式,是python自动化中常见的一种数据驱动的方式

1)读取数据:

# 方法一(推荐)
filename = r'D:\stdutyZiLiao\pythoneProjects\webUI\逻辑流程\test006.yaml'
with open(file=filename,mode='r+',encoding='utf-8') as f :# yaml.load()all_info = yaml.load(f,Loader = yaml.Loader)print(all_info) # {'key': '字典', 'msg': {'key1': 'value1', 'key2': 'value2'}, 'info': [1, 2, 3, 4, 6]}# 方法二
f = open(file=filename,mode='r+',encoding='utf-8')
all_info = yaml.load(f,Loader = yaml.Loader)
print(all_info)
f.close()

2)写入数据:

# 写入数据
# 1.数据源
# 2.写入到什么文件
TestCase = {'case1':{'casename': '登陆成功用例','username': 'xx1','pwd': '12345','msg': '登录成功'},'case2':{'casename': '登陆失败用例','username': 'xxxxx1','pwd':'12345','msg':'登录失败'}
}
with open(file=filename,mode='w+',encoding='utf-8') as f:yaml.dump(TestCase,f,allow_unicode=True) # 是否支持unicode格式:是

关键字:python学习14:如何读取yaml文件?

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: