yaml数据读取
yaml在项目中存储数据的方式,数据层次清晰,可以跨平台,可以基于不同语言,转化速度快
准备操作:引入Python第三方库YAML
File-Setting-projrct:python interpreter,点击 + 号,搜索pyYAML,进行安装
yaml数据如下
读取YAML数据
import os.path
import yaml
def readconfigyaml(path): # 读取yaml数据
with open(path, "r+", encoding="UTF-8") as file:
data = yaml.load(stream=file, Loader=yaml.FullLoader)
return data
if __name__ == '__main__':
rootPath = os.path.abspath(os.path.dirname(os.path.dirname(__file__))) # dirname获取上一级路劲
print(rootPath)
path = os.path.join(rootPath, "config\config.yaml")
print(readconfigyaml(path))
运行结果如下