博客
关于我
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/

你可能感兴趣的文章
NOPI读取Excel
查看>>
NoSQL&MongoDB
查看>>
NoSQL介绍
查看>>
NoSQL数据库概述
查看>>
Notadd —— 基于 nest.js 的微服务开发框架
查看>>
NOTE:rfc5766-turn-server
查看>>
Notepad ++ 安装与配置教程(非常详细)从零基础入门到精通,看完这一篇就够了
查看>>
Notepad++在线和离线安装JSON格式化插件
查看>>
notepad++最详情汇总
查看>>
notepad++正则表达式替换字符串详解
查看>>
notepad如何自动对齐_notepad++怎么自动排版
查看>>
Notes on Paul Irish's "Things I learned from the jQuery source" casts
查看>>
Notification 使用详解(很全
查看>>
NotImplementedError: Cannot copy out of meta tensor; no data! Please use torch.nn.Module.to_empty()
查看>>
NotImplementedError: Could not run torchvision::nms
查看>>
nova基于ubs机制扩展scheduler-filter
查看>>
Now trying to drop the old temporary tablespace, the session hangs.
查看>>
nowcoder—Beauty of Trees
查看>>
np.arange()和np.linspace()绘制logistic回归图像时得到不同的结果?
查看>>
np.power的使用
查看>>