博客
关于我
yaml数组类型
阅读量:355 次
发布时间:2019-03-04

本文共 710 字,大约阅读时间需要 2 分钟。

在处理yaml文件时,数组类型的表示方式和转换至列表类型的过程可能会让一些开发者感到困惑。本文将详细解释yaml数组类型的表示方法以及如何在Python中将其转换为列表类型。

在yaml中,数组通常使用带有破折号的行来表示。以下是一个常见的yaml数组表达式:

- 100- 1- 2- 3

在这个例子中,每一行前面都带有破折号 -,表示这些值构成一个数组。如果你想将其转换为Python中的列表类型,可以使用yaml库来解析。以下是一个示例代码:

import yamlimport osproject_path = os.path.split(os.path.realpath(__file__))[0].split('tools')[0]  # 项目路径def get_yaml_data(fileDir):    # 在内存中加载文件    f = open(fileDir, 'r', encoding='utf-8')    # 使用FullLoader加载yaml文件    res = yaml.load(f, Loader=yaml.FullLoader)    return resif __name__ == '__main__':    info = get_yaml_data(project_path + r'\configs\cnf.yaml')    print(info)

运行上述代码后,输出结果会是:

- [100, [1, 2, 3]]

这个结果表明,原始yaml文件中的数组类型已经成功转换为Python中的列表类型。通过这种方式,你可以方便地在代码中使用yaml文件中的数据。

转载地址:http://wjeh.baihongyu.com/

你可能感兴趣的文章
npm build报错Cannot find module ‘webpack‘解决方法
查看>>
npm ERR! ERESOLVE could not resolve报错
查看>>
npm ERR! fatal: unable to connect to github.com:
查看>>
npm ERR! Unexpected end of JSON input while parsing near '...on":"0.10.3","direc to'
查看>>
npm ERR! Unexpected end of JSON input while parsing near ‘...“:“^1.2.0“,“vue-html-‘ npm ERR! A comp
查看>>
npm error Missing script: “server“npm errornpm error Did you mean this?npm error npm run serve
查看>>
npm error MSB3428: 未能加载 Visual C++ 组件“VCBuild.exe”。要解决此问题,1) 安装
查看>>
npm install CERT_HAS_EXPIRED解决方法
查看>>
npm install digital envelope routines::unsupported解决方法
查看>>
npm install 卡着不动的解决方法
查看>>
npm install 报错 EEXIST File exists 的解决方法
查看>>
npm install 报错 ERR_SOCKET_TIMEOUT 的解决方法
查看>>
npm install 报错 Failed to connect to github.com port 443 的解决方法
查看>>
npm install 报错 fatal: unable to connect to github.com 的解决方法
查看>>
npm install 报错 no such file or directory 的解决方法
查看>>
npm install 权限问题
查看>>
npm install报错,证书验证失败unable to get local issuer certificate
查看>>
npm install无法生成node_modules的解决方法
查看>>
npm install的--save和--save-dev使用说明
查看>>
npm node pm2相关问题
查看>>