yaml文件读取load()、写入dump()

读取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)

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文件读取load()、写入dump()_第1张图片

 

 

写入: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文件读取load()、写入dump()_第2张图片

标记:

yaml文件读取load()、写入dump()_第3张图片

读取文件时,运行结果:yaml文件读取load()、写入dump()_第4张图片

 

 

整理:

yaml文件读取load()、写入dump()_第5张图片

你可能感兴趣的:(appium)