python_配置文件_yaml

一.

配置文件:

定义:对项目进行配置管理。

一般用来对可变参数进行配置(如:环境地址)

表现形式:.yaml 或者 .yml  结尾的文件

支持的数据类型(读取与存储):

  • 字符串
  • 整型
  • 浮点型
  • 布尔型
  • null
  • 时间
  • 日期

1.安装

pip install pyyaml

国内安装源:

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pyyaml

2.使用

注意:

字典的表示方法(“log”:"test"),列表的表示方法(- list) ,字符串可以用空格("log": test);

yaml文件中缩进必须用空格进行;

1)读取数据

yaml 文件内容:

 

 

#读取数据
with open('conf.yaml',encoding='utf-8') as f:
conf=yaml.load(f,Loader=yaml.SafeLoader)
print(conf)
运行结果:

 

 

2)写入数据

# 写入yaml
with open("new.yaml", 'w', encoding='utf8') as f:
yaml.dump({"new_yaml": "write"}, f)
运行结果:

 更多yaml相关知识:https://www.jb51.net/article/184299.htm

posted @ 2020-05-31 12:22  1142783691  阅读(315)  评论(0编辑  收藏  举报