yaml.load() 函数


import os
import yaml

def get_yaml_data(yaml_file):
    # 打开yaml文件
    print('打开yaml文件数据')
    file=open(yaml_file,'r',encoding='utf-8')
    file_data=file.read()
    file.close()

    print(file_data)
    print('类型',type(file_data))

    # 将字符串转换为字典或者列表
    print('转换yaml数据为字典或者列表')
    data=yaml.load(file_data,Loader=yaml.Loader)
    print(data)
    print('类型',type(data))

    return data

current_path=os.path.join('.')
yaml_path=os.path.join(current_path,'1.yaml')

get_yaml_data(yaml_path)

#yaml键值对相当于字典
epoch: 100
# 键值对嵌套,相当于字典嵌套字典

user1:
  name: a
  psw: 123
user2:
  name: b
  psw: 456

#键值对中嵌套数组
user3:
  - a
  - b
  - c
  - d

猜你喜欢

转载自blog.csdn.net/qq_40107571/article/details/134657362