
YAML 支持三种集合类型:列表,映射和集。
“-”表示;“-”:fruit:
- apple
- rubber
- pearPyyaml解析:# 解析
with open("config_jihe.yaml") as f:
yaml_data2 = yaml.load(f, Loader=yaml.FullLoader)
print(yaml_data2)
# 输出
{'fruit': ['apple', 'rubber', 'pear']}“:”表示;fruit:
- apple
- rubber
- pear
vegetable:
green: cucumber
red: tomato
yellow: cornPyyaml解析:{'fruit': ['apple', 'rubber', 'pear'], 'vegetable': {'green': 'cucumber', 'red': 'tomato', 'yellow': 'corn'}}“{}”表示;tree: {poplar, willow, pine}{'tree': {'poplar': None, 'willow': None, 'pine': None}}vegetable:
green: cucumber
red: tomato
yellow: corn
tree:
one: poplar
two: willow
three: pine{'vegetable': {'green': 'cucumber', 'red': 'tomato', 'yellow': 'corn'}, 'tree': {'one': 'poplar', 'two': 'willow', 'three': 'pine'}}data: {vegetable: {green: cucumber, red: tomato, yellow: corn}, tree: {one: poplar, two: willow, three: pine}}{'data': {'vegetable': {'green': 'cucumber', 'red': 'tomato', 'yellow': 'corn'}, 'tree': {'one': 'poplar', 'two': 'willow', 'three': 'pine'}}}null 值等;# 字符串:在双引号中使用转义符号来表示特殊字符
str: "Hello,\\nWorld!"
# 数字:可以表示整数和浮点数
int: 88888
float: 3.141592653
# 布尔值:可以使用true和false表示
boolean: true
# null 值:使用 null 来表示空值
empty: nullPyyaml解析:{'str': 'Hello,\\nWorld!', 'int': 88888, 'float': 3.141592653, 'boolean': True, 'empty': None}# 单引号:表示精确字符串,不会进行转义
single: 'Hello,\nworld!'
# 双引号:表示标准字符串,可以进行转义
double: "Hello,\\nworld!"
# 无引号:可以识别特殊字符,但空格会自动被转义成字符串
none: hello world{'single': 'Hello,\\nworld!', 'double': 'Hello,\\nworld!', 'none': 'hello world'}# !!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: 100YAML文档内容的特殊语义:编号 | 字符 | 功能 |
|---|---|---|
1 |
| 表示块序列条目 |
2 |
| 表示映射键 |
3 |
| 表示映射值 |
4 |
| 表示流集合条目 |
5 |
| 开始流序列 |
6 |
| 结束流序列 |
7 |
| 启动流映射 |
8 |
| 结束流映射 |
9 |
| 表示注释 |
10 |
| 表示节点的锚属性 |
11 |
| 表示别名节点 |
12 |
| 表示节点的标签 |
13 |
| 表示一个字面块标量 |
14 |
| 表示折叠块标量 |
15 |
| 单引号围绕引用的流标量 |
16 |
| 双引号包围双引号流标量 |
17 |
| 表示使用的指令 |
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。