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)
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"
运行结果:
写入: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
运行结果:
标记:
读取文件时,运行结果:
整理:
上一篇: win7单个文件夹加密码的方法
下一篇: Maven 入门 —— 创建打包脚本