简友广场散文

7.Python读取yaml文件封装

2022-11-15  本文已影响0人  小小一颗卤蛋

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支持的数据类型:

列表

- 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
上一篇 下一篇

猜你喜欢

热点阅读