欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

yaml文件读取、写入

程序员文章站 2022-03-20 09:05:12
...

读取yaml、yml文件:load()

data=yaml.load(f,Loader=yaml.FullLoader)

要加上:Loader=yaml.FullLoader,否则会报错:YAMLLoadWarning: calling yaml.load() without Loader=... is deprecated, as the default Loader is unsafe. Please read https://msg.pyyaml.org/load for full details.
  data=yaml.load(f)

yaml文件读取、写入

import yaml

def main():
    with open("./data.yaml","r") as f:
    data=yaml.load(f,Loader=yaml.FullLoader)
    print(data)

if __name__ == '__main__':
    main()

data.yaml文件:

#字典
name: "hello"

运行结果:

yaml文件读取、写入

 

 

写入:dump()

data={"S_data":{"test1":"hello"},"Sdata2":{"name":"汉字"}}

encoding='utf-8',allow_unicode=True

#coding=gbk
import yaml

def main():
    #写入数据:
    data={"S_data":{"test1":"hello"},"Sdata2":{"name":"汉字"}}
    with open("./data.yaml","w") as f:
        yaml.dump(data,f,encoding='utf-8',allow_unicode=True)
if __name__ == '__main__':
    main()

data数据中有汉字时,加上:encoding='utf-8',allow_unicode=True

运行结果:

yaml文件读取、写入

标记:

yaml文件读取、写入

读取文件时,运行结果:yaml文件读取、写入

 

 

整理:

yaml文件读取、写入

相关标签: appium