7.Python读取yaml文件封装 yaml读取文件封装 yaml基础知识

yaml读取文件封装

yaml文件读取封装,需要传入文件参数来获取文件数据

import yaml 
def get_yaml_data(filpath:str):
  with open(file_path,encoding='utf-8') as fo: # file_object
     return yaml.self(fo.read())
if __name__='__main__':
  res = get_yaml_data('../configs/apiPathConfig.yaml') # yaml文件的位置
  print(res)

yaml基础知识

yaml支持的数据类型:

  • 对象,键值对,又称为映射,字典,哈希
  • 数组,一组按次序排列的值,又称序列,列表
  • 纯量,单个的,不可分割的值,如字符串,布尔值,整数,浮点数,null,时间,日期

列表

- 10
- 20
- 30
- 40
- 50

字典

name: dehua
age: 20

列表中的字典

-
name: tom
age: 21
-
A: apple

字典中的列表

name:
- 张三
- 李四
- 王五
age: 20

字典中的字典

name:
name1: qingyun

多段内容,用---分隔

- 10
- 20
---
name: dehua
age: 20

变量:
&变量名 定义变量
*变量名 使用变量

HOST: &HOST 121.41.14.39
url: *HOST
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章