欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python读取yaml配置文件

程序员文章站 2022-03-30 08:17:11
安装yamlpip install PyYAML读取def read_yaml(): path = 'yaml_ini_data.yaml' # print(path) file = open(path, 'rb') result = file.read() data = yaml.load(result, Loader=yaml.FullLoader) file.close() return datayaml数据类型list: -...

安装yaml

pip install PyYAML

读取

def read_yaml():
    path = 'yaml_ini_data.yaml'
    # print(path)
    file = open(path, 'rb')
    result = file.read()
    data = yaml.load(result, Loader=yaml.FullLoader)
    file.close()
    return data

yaml数据类型

list:
  - 'list_1'
  - 'list_2'
  - 'list_3'
  - 'list_4'
  - 'list_5'
  - 'list_6'
dict: 'dict'
复杂字典:
dict_data :
  'address': 'test'
  'raw' : 'test'
  'coinCode': 'test'
  'amount': 1000
  'betSite':
    '21' : 1000
    
等同于json:
{
    "address": "test",
    "raw": "test",
    "coinCode": "test",
    "amount": 1000,
    "betSite": {
        "test": "1000"
    }
}

暂时只用到这几种,其他以后用到再写

本文地址:https://blog.csdn.net/weixin_42132508/article/details/107162171