博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
(译)yaml快速教程
阅读量:5024 次
发布时间:2019-06-12

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

原文地址:


YAML是一种旨在直接可由人类编写和读取的数据序列化语言。

它是JSON的严格超集,语法上添加了类似Python的换行符和缩进。 但是与Python不同,YAML不允许使用TAB缩进。

---  # 文档开始# 这是注释###########  标量类型 ########### 我们的根对象(包含整个文档)是一个map,# 等价于其他语言中的dictionary, hash 或 objectkey: value # 键值对another_key: Another value goes here. # 另一个键值对a_number_value: 100 # 数值scientific_notation: 1e+12 # 科学计数法表示的数值boolean: true # 布尔值null_value: null # null值key with spaces: value # 带空格的键# 注意字符串不需要引号,但可以使用引号however: 'A string, enclosed in quotes.' # 用引号括起来的字符串'Keys can be quoted too.': "Useful if you want to put a ':' in your key." # 键也可以括起来single quotes: 'have ''one'' escape pattern' # 使用单引号double quotes: "have many: \", \0, \t, \u263A, \x0d\x0a == \r\n, and more." # 使用双引号# 多行字符串可以采用“文本块” (使用 |),# 或“折叠块”(使用 >)literal_block: |    This entire block of text will be the value of the 'literal_block' key,    with line breaks being preserved.    The literal continues until de-dented, and the leading indentation is    stripped.        Any lines that are 'more-indented' keep the rest of their indentation -        these lines will be indented by 4 spaces.# 整个文本块将是'literal_block'键的值,保留换行符。# 直到进一步缩进时,所有前导缩进将会被去除。#     存在“更多缩进”的行将保留其缩进的剩余部分 - 这两行好将缩进4个空格。# # 示例结果:# This entire block of text will be the value of the 'literal_block' key,# with line breaks being preserved.# # The literal continues until de-dented, and the leading indentation is# stripped.# #     Any lines that are 'more-indented' keep the rest of their indentation -#     these lines will be indented by 4 spaces.folded_style: >    This entire block of text will be the value of 'folded_style', but this    time, all newlines will be replaced with a single space.    Blank lines, like above, are converted to a newline character.        'More-indented' lines keep their newlines, too -        this text will appear over two lines.# 折叠风格可以将(空格分割的)长行在空格处打断为多行,其特点是:# - 折叠的换行符替换为空格# - 更多缩进的行不折叠# (译者注:这里,以及官方文档,都没能将其规则说清楚,为什么空行消失。但一般不会遇到这么复杂的字符串,所以不用深究。)# ## 示例结果:# This entire block of text will be the value of 'folded_style', but this time, all newlines will be replaced with a single space.# Blank lines, like above, are converted to a newline character.# #     'More-indented' lines keep their newlines, too -#     this text will appear over two lines.########### 集合类型 ############ 使用缩进嵌套,推荐2个空格(但非必须)a_nested_map:  key: value  another_key: Another Value  another_nested_map:    hello: hello# map不一定需要字符串的键0.25: a float key# 键可以很复杂,如多行对象。# 使用 ? 及其后的一个空格,表明复杂键的开始? |  This is a key  that has multiple lines: and this is its value# YAML同样允许序列之间映射,使用复杂键的语法。# 一些实现不支持这样做,如jackson。? - Manchester United  - Real Madrid: [2001-01-01, 2002-02-02]# 序列(等价于列表或数组)如下所示# (注意'-'记作一个缩进):a_sequence:  - Item 1  - Item 2  - 0.5  # 序列支持不同类型  - Item 4  - key: value    another_key: another_value  -    - This is a sequence    - inside another sequence  - - - Nested sequence indicators      - can be collapsed# 鉴于YAML是JSON的超集,可以使用JSON风格的map和序列:json_map: {"key": "value"}json_seq: [3, 2, 1, "takeoff"]and quotes are optional: {key: [3, 2, 1, takeoff]}############额外的功能 ############ YAML拥有称为“锚”的便捷功能,可以在文档中复用内容。下两个key拥有共同的值:anchored_content: &anchor_name This string will appear as the value of two keys.other_anchor: *anchor_name# 锚可用于复制/继承属性base: &base  name: Everyone has same name# The regexp << is called Merge Key Language-Independent Type. It is used to# indicate that all the keys of one or more specified maps should be inserted# into the current map.foo: &foo  <<: *base  age: 10bar: &bar  <<: *base  age: 20# foo and bar would also have name: Everyone has same name# YAML还有标记,您可以使用它来显式声明类型。explicit_string: !!str 0.5# Some parsers implement language specific tags, like this one for Python's# complex number type.python_complex_number: !!python/complex 1+2j# We can also use yaml complex keys with language specific tags? !!python/tuple [5, 7]: Fifty Seven# Would be {(5, 7): 'Fifty Seven'} in Python################# 额外的YAML类型 ################## 除了字符串和数字值,YAML还支持ISO格式的date和datetime字面量。datetime: 2001-12-15T02:59:43.1Zdatetime_with_spaces: 2001-12-14 21:59:43.10 -5date: 2002-12-14# "!!binary"标签声明一个字符串是base64编码的,表示一个二进制blob# (译者注:参考Python相关概念)gif_file: !!binary |  R0lGODlhDAAMAIQAAP//9/X17unp5WZmZgAAAOfn515eXvPz7Y6OjuDg4J+fn5  OTk6enp56enmlpaWNjY6Ojo4SEhP/++f/++f/++f/++f/++f/++f/++f/++f/+  +f/++f/++f/++f/++f/++SH+Dk1hZGUgd2l0aCBHSU1QACwAAAAADAAMAAAFLC  AgjoEwnuNAFOhpEMTRiggcz4BNJHrv/zCFcLiwMWYNG84BwwEeECcgggoBADs=# YAML拥有一个集合类型:set:  ? item1  ? item2  ? item3or: {item1, item2, item3}# 集合只是简单的映射为空值,以上等价于:set2:  item1: null  item2: null  item3: null...  # 文档结束

转载于:https://www.cnblogs.com/redreampt/p/10753826.html

你可能感兴趣的文章
Redis的Pub/Sub客户端实现
查看>>
SQL日常问题和技巧——持续更新
查看>>
springMVC入门(一)------springMVC基本概念与安装
查看>>
Sam做题记录
查看>>
[bzoj] 2453 维护数列 || 单点修改分块
查看>>
IIS版本变迁
查看>>
BZOJ3884: 上帝与集合的正确用法 拓展欧拉定理
查看>>
mybatis09--自连接一对多查询
查看>>
myeclipse10添加jQuery自动提示的方法
查看>>
【eclipse jar包】在编写java代码时,为方便编程,常常会引用别人已经实现的方法,通常会封装成jar包,我们在编写时,只需引入到Eclipse中即可。...
查看>>
视频监控 封装[PlayCtrl.dll]的API
查看>>
软件工程APP进度更新
查看>>
Python 使用正则替换 re.sub
查看>>
CTF中那些脑洞大开的编码和加密
查看>>
简化工作流程 10款必备的HTML5开发工具
查看>>
c++ 调用外部程序exe-ShellExecuteEx
查看>>
Java进击C#——语法之知识点的改进
查看>>
IdentityServer流程图与相关术语
查看>>
BirdNet: a 3D Object Detection Framework from LiDAR information
查看>>
icon fonts入门
查看>>