import yaml import os def get_yaml_data(yaml_file): reslist = [] # 打开yaml文件 with open(yaml_file, 'r', encoding="utf-8") as f: file_data = f.read() print("读yaml文件,内容格式与文档一致:", file_data) print("打印的类型为str:", type(file_data)) # 将字符串转化为字典 data = yaml.safe_load(file_data) # data = yaml.load((file_data),Loader=yaml.FullLoader) print("将yaml文件转为字典:", data) print("打印的类型为dict:", type(data)) # 获取具体字段内容,可一层一层往下取值 res = data['DevService'] print(res) current_path = "D:\xxx\xxx\data" yaml_path = os.path.join(current_path, "apiConfigs.yaml") get_yaml_data(yaml_path)
来源:https://www.jianshu.com/p/eaa1bf01b3a6