YAML 是一个可读性高,用来表达资料序列化的格式。

简介
- YAML(/ˈjæməl/) 是一个可读性高,用来表达资料序列化的格式。
- YAML的语法和其他高阶语言类似,并且可以简单表达清单、散列表,纯量等数据格式。
- 文件扩展名为
.yaml, .yml
- 官方网站:http://yaml.org/
语言优点
- YAML易于人们阅读。
- YAML数据在编程语言之间是可移植的。
- YAML匹配敏捷语言的本机数据结构。
- YAML具有一致的模型来支持通用工具。
- YAML支持单程处理。
- YAML具有表现力和可扩展性。
- YAML易于实现和使用。
发行版本
| 版本 |
发布日期 |
| YAML 1.0 |
29 January 2004 |
| YAML 1.1 |
18 January 2005 |
| YAML 1.2.0 |
21 July 2009 |
| YAML 1.2.1 |
1 October 2009 |
| YAML 1.2.2 |
1 October 2021 |
示例文件
- 数据可以用类似大纲的缩进排序方式呈现,和
Python 的缩进要求很相似
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| --- receipt: Oz-Ware Purchase Invoice date: 2012-08-06 customer: given: Dorothy family: Gale items: - part_no: A4786 descrip: Water Bucket (Filled) price: 1.47 quantity: 4 - part_no: E1628 descrip: High Heeled "Ruby" Slippers size: 8 price: 133.7 quantity: 1 bill-to: &id001 street: | 123 Tornado Alley Suite 16 city: East Centerville state: KS ship-to: *id001 specialDelivery: > Follow the Yellow Brick Road to the Emerald City. Pay no attention to the man behind the curtain. ...
|
核心语法
- **分层:**使用空格
Space 缩进表示分层,不同层次之间的缩进可以使用不同的空格数目,但是同层元素一定左对齐,即前面空格数目相同(不能使用 Tab,各个系统 Tab对应的 Space 数目可能不同,导致层次混乱)
- 注释:
# 表示注释,只能单行注释,从 # 开始处到行尾
- 列表: 破折号后面跟一个空格(a dash and space)表示列表
- **键值对:**用冒号和空格表示键值对
key: value
- 数据: 简单数据(scalars,标量数据)可以不使用引号括起来,包括字符串数据。用单引号或者双引号括起来的被当作字符串数据,在单引号或双引号中使用C风格的转义字符
- 大小写敏感
基本用法
缩进
- 以空格的缩进来控制层级关系;只要是左对齐的一列数据,都是同一个层级的:
1 2 3
| server: port: 8081 path: /hello
|
属性和值大小写敏感。
字符串
1
| name: ‘zhangsan \n lisi’:输出;zhangsan \n lisi
|
对象
数组
1 2 3 4
| pets: - cat - dog - pig
|
数组也可以有行内写法:
-
按照这个规则,数组是可以嵌套的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| age: 23 items: - age: 15 name: AA sub_items: - link: baidu name: test - link: google name: foo - age: 66 name: BB sub_items: - link: opencv name: bar - link: halcon name: tt
|
数据类型
-
yaml 中有以下基本类型:
- 字符串
- 整型
- 浮点型
- 布尔型
- null
- 时间
- 日期
-
示例:
1 2 3 4 5 6 7
| str: "Hello World!" int: 110 float: 3.141 boolean: true # or false None: null # 也可以用 ~ 号来表示 null time: 2016-09-22t11:43:30.20+08:00 # ISO8601,写法百度 date: 2016-09-22 # 同样ISO8601
|
引用
-
& 和 * 用于引用
1 2
| name: &name 灰蓝 tester: *name
|
这个相当于以下脚本:
强制转换
- yaml 是可以进行强制转换的,用
!! 实现,如下:
1 2
| str: !!str 3.14 int: !!int "123"
|
输出:
1
| {'int': 123, 'str': '3.14'}
|
明显能够看出123被强转成了int类型,而float型的3.14则被强转成了str型。
分段
Python 读写
- Python 可以方便地读写 yaml 文件
- 引入库
import yaml
- 读 yml
yaml.load(f)
- 写 yml
yaml.dump(new_date, f)
- 示例代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| import yamlnew_date = { "name": "Tom", "age": 23, "sex": "man", 'items':[ { 'name': 'AA', 'age': 15, 'sub_items': [ { 'name': 'test', 'link': 'baidu' }, { 'name': 'foo', 'link': 'google' } ] }, { 'name': 'BB', 'age': 66, 'sub_items': [ { 'name': 'bar', 'link': 'opencv' }, { 'name': 'tt', 'link': 'halcon' } ] } ] } f = open("test.yaml", "w") yaml.dump(new_date, f) f.close() f = open("test.yaml", "r") loaded_yml_obj = yaml.load(f) f.close() print(loaded_yml_obj)
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| age: 23 items: - age: 15 name: AA sub_items: - link: baidu name: test - link: google name: foo - age: 66 name: BB sub_items: - link: opencv name: bar - link: halcon name: tt name: Tom sex: man
|
1
| {'age': 23, 'items': [{'age': 15, 'name': 'AA', 'sub_items': [{'link': 'baidu', 'name': 'test'}, {'link': 'google', 'name': 'foo'}]}, {'age': 66, 'name': 'BB', 'sub_items': [{'link': 'opencv', 'name': 'bar'}, {'link': 'halcon', 'name': 'tt'}]}], 'name': 'Tom', 'sex': 'man'}
|
参考资料
文章链接:
https://www.zywvvd.com/notes/tools/yml/yml-usage/