首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >YAML+PyYAML笔记 3 | YAML集合、结构、标量、标记使用

YAML+PyYAML笔记 3 | YAML集合、结构、标量、标记使用

原创
作者头像
虫无涯
发布2023-07-28 09:41:59
发布2023-07-28 09:41:59
8290
举报
文章被收录于专栏:全栈测试技术全栈测试技术

1 集合

YAML 支持三种集合类型:列表,映射和集。

1.1 列表

  • 列表是一种序列结构,它使用连字符“-”表示;
  • 如下三个元素的列表,元素之间用“-”
代码语言:python
复制
fruit:
 - apple
 - rubber
 - pear
  • 使用Pyyaml解析:
代码语言:python
复制
# 解析
with open("config_jihe.yaml") as f:
    yaml_data2 = yaml.load(f, Loader=yaml.FullLoader)
print(yaml_data2)

# 输出
{'fruit': ['apple', 'rubber', 'pear']}

1.2 映射

  • 即一种键值对结构,它使用冒号“:”表示;
  • 如下:
代码语言:python
复制
fruit:
  - apple
  - rubber
  - pear

vegetable:
  green: cucumber
  red: tomato
  yellow: corn
  • 使用Pyyaml解析:
代码语言:python
复制
{'fruit': ['apple', 'rubber', 'pear'], 'vegetable': {'green': 'cucumber', 'red': 'tomato', 'yellow': 'corn'}}

1.3 集

  • 无序不重复的数据类型;
  • 用大括号“{}”表示;
代码语言:python
复制
tree: {poplar, willow, pine}
代码语言:python
复制
{'tree': {'poplar': None, 'willow': None, 'pine': None}}

2 结构

  • 可用于任何数据类型的复杂性结构;
  • 包括集合和其他数据类型。

2.1 多行结构

  • 表示复杂数据类型的方式;
  • 如下:
代码语言:python
复制
vegetable:
  green: cucumber
  red: tomato
  yellow: corn

tree:
  one: poplar
  two: willow
  three: pine
代码语言:python
复制
{'vegetable': {'green': 'cucumber', 'red': 'tomato', 'yellow': 'corn'}, 'tree': {'one': 'poplar', 'two': 'willow', 'three': 'pine'}}

2.2 单行结构

  • 在一行上表示复杂的结构:
  • 如下:
代码语言:python
复制
data: {vegetable: {green: cucumber, red: tomato, yellow: corn}, tree: {one: poplar, two: willow, three: pine}}
代码语言:python
复制
{'data': {'vegetable': {'green': 'cucumber', 'red': 'tomato', 'yellow': 'corn'}, 'tree': {'one': 'poplar', 'two': 'willow', 'three': 'pine'}}}

3 字面量

  • 字面量可以表示字符串、数字、布尔值、null 值等;
代码语言:python
复制
# 字符串:在双引号中使用转义符号来表示特殊字符
str: "Hello,\\nWorld!"

# 数字:可以表示整数和浮点数
int: 88888
float: 3.141592653

# 布尔值:可以使用true和false表示
boolean: true

# null 值:使用 null 来表示空值
empty: null
  • 使用Pyyaml解析:
代码语言:python
复制
{'str': 'Hello,\\nWorld!', 'int': 88888, 'float': 3.141592653, 'boolean': True, 'empty': None}

4 标量

  • 分为单引号、双引号和无引号;
代码语言:python
复制
# 单引号:表示精确字符串,不会进行转义
single: 'Hello,\nworld!'

# 双引号:表示标准字符串,可以进行转义
double: "Hello,\\nworld!"

# 无引号:可以识别特殊字符,但空格会自动被转义成字符串
none: hello world
代码语言:python
复制
{'single': 'Hello,\\nworld!', 'double': 'Hello,\\nworld!', 'none': 'hello world'}

5 标记

代码语言:python
复制
# !!str:表示字符串类型。如:
key: !!str string

# !!int:表示整数类型。如:
key: !!int 123

# !!float:表示浮点数类型。如:
key: !!float 3.14

# !!bool:表示布尔类型。如:
key: !!bool true

# &name:为数据定义一个锚点,可以在后面使用锚点引用。如:
person: &p
name: xiaozhang
age: 88

student:
<<: *p
grade: 100

6 指示符

  • 指示符包括用于描述YAML文档内容的特殊语义:

编号

字符

功能

1

_

表示块序列条目

2

?

表示映射键

3

:

表示映射值

4

,

表示流集合条目

5

[

开始流序列

6

]

结束流序列

7

{

启动流映射

8

}

结束流映射

9

#

表示注释

10

&

表示节点的锚属性

11

*

表示别名节点

12

!

表示节点的标签

13

Ι

表示一个字面块标量

14

>

表示折叠块标量

15

'

单引号围绕引用的流标量

16

"

双引号包围双引号流标量

17

%

表示使用的指令

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1 集合
    • 1.1 列表
    • 1.2 映射
    • 1.3 集
  • 2 结构
    • 2.1 多行结构
    • 2.2 单行结构
  • 3 字面量
  • 4 标量
  • 5 标记
  • 6 指示符
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档