YAML

Instruction

YAML 是一种比其他常见数据格式(如XML或JSON)更易于读写的文件格式。

official reference

Collections

YAML的块集合使用缩进限定范围并在其自己的行上开始每个元素。序列用短划线和空格- 表示每个元素。映射使用冒号和空格来标记每个键值对。注释以井号开头(也称为octothorpehashsharppoundnumber sign - )。

List

- Mark McGwire
- Sammy Sosa
- Ken Griffey

Map

hr:  65    # Home runs
avg: 0.278 # Batting average
rbi: 147   # Runs Batted In

Map[List]

american:
  - Boston Red Sox
  - Detroit Tigers
  - New York Yankees
national:
  - New York Mets
  - Chicago Cubs
  - Atlanta Braves

List[Map]


-
  name: Mark McGwire
  hr:   65
  avg:  0.278
-
  name: Sammy Sosa
  hr:   63
  avg:  0.288

List[List]

- [name        , hr, avg  ]
- [Mark McGwire, 65, 0.278]
- [Sammy Sosa  , 63, 0.288]

Map[Map]

Mark McGwire: {hr: 65, avg: 0.278}
Sammy Sosa: {
    hr: 63,
    avg: 0.288
  }

Structures

YAML使用三个破折号---将指令与文档内容分开。如果没有指令,这也用于表示文档的开始。三个点...表示文档的结尾而不开始新的文档,用于通信通道。

Two Documents in a Stream (each with a leading comment)

# Ranking of 1998 home runs
---
- Mark McGwire
- Sammy Sosa
- Ken Griffey

# Team ranking
---
- Chicago Cubs
- St Louis Cardinals

Play by Play Feed from a Game

---
time: 20:03:20
player: Sammy Sosa
action: strike (miss)
...
---
time: 20:03:47
player: Sammy Sosa
action: grand slam
...

Single Document with Two Comments


---
hr: # 1998 hr ranking
  - Mark McGwire
  - Sammy Sosa
rbi:
  # 1998 rbi ranking
  - Sammy Sosa
  - Ken Griffey

Node for “Sammy Sosa” appears twice in this document

重复节点(对象)首先由锚点符号+alias标记,然后用*引用别名。

---
hr:
  - Mark McGwire
  # Following node labeled SS
  - &SS Sammy Sosa
rbi:
  - *SS # Subsequent occurrence
  - Ken Griffey

Mapping between Sequences - Map[List,List]

问号和空格? 表示复杂映射的key。在块集合中,key:value可以在短划线-,冒号:或问号?后立即开始。

? - Detroit Tigers
  - Chicago cubs
:
  - 2001-07-23

? [ New York Yankees,
    Atlanta Braves ]
: [ 2001-07-02, 2001-08-12,
    2001-08-14 ]

Compact Nested Mapping - List[Map]

---
# Products purchased
- item    : Super Hoop
  quantity: 1
- item    : Basketball
  quantity: 4
- item    : Big Shoes
  quantity: 1

Scalars

标量内容可以用块表示法编写,使用文字样式(用|表示),其中所有换行符都很重要。或者,它们可以用折叠样式(用>表示)书写,其中每个换行符都会被转换成一个空格,除非它以空行结束或下一行有更多缩进。

In literals, newlines are preserved

在文字中保留换行符

# ASCII Art
--- |
  \//||\/||
  // ||  ||__
  
# - 表示删除字符串末尾的换行符
s1: |-
  Foo
  
# + 表示保留文字块末尾的换行符 
s2: |+
  Foo
  

In the folded scalars, newlines become spaces

在折叠的标量中,换行符会转换成空格

--- >
  Mark McGwire's
  year was crippled
  by a knee injury.

Folded newlines are preserved for “more indented” and blank lines

折叠空行保留为更多的缩进和空白行

>
 Sammy Sosa completed another
 fine season with great stats.

   63 Home Runs
   0.288 Batting Average

 What a year!

Indentation determines scope

缩进确定范围

name: Mark McGwire
accomplishment: >
  Mark set a major league
  home run record in 1998.
stats: |
  65 Home Runs
  0.278 Batting Average

Quoted Scalars - 引号标量

YAML的流量标量包括普通样式(迄今为止的大多数示例)和两种引用样式。双引号样式提供转义序列。当不需要转义时,单引号样式很有用。

unicode: "Sosa did fine.\u263A"
control: "\b1998\t1999\t2000\n"
hex esc: "\x0d\x0a is \r\n"

single: '"Howdy!" he cried.'
quoted: ' # Not a ''comment''.'
tie-fighter: '|\-*-/|'

Multi-line Flow Scalars

所有流量标量都可以跨越多行,换行总是折叠的。

plain:
  This unquoted scalar
  spans many lines.

quoted: "So does this
  quoted scalar.\n"
  

Tags

在YAML中,未标记节点的数据类型由应用程序给出。本规范中的示例通常使用故障安全模式中的seq,map和str类型。一些示例还使用JSON模式中的int,float和null类型。存储库包括其他类型,如binary,omap(ordered map),set等。

Integers

canonical: 12345
decimal: +12345
octal: 0o14
hexadecimal: 0xC

Floating Point

canonical: 1.23015e+3
exponential: 12.3015e+02
fixed: 1230.15
negative infinity: -.inf
not a number: .NaN

Miscellaneous - 杂项

null:
booleans: [ true, false ]
string: '012345'

Timestamps

canonical: 2001-12-15T02:59:43.1Z
iso8601: 2001-12-14t21:59:43.10-05:00
spaced: 2001-12-14 21:59:43.10 -5
date: 2002-12-14

Various Explicit Tags - 各种显示标签

使用感叹号!符号用标记表示显式类型。

---
not-date: !!str 2002-04-28

picture: !!binary |
 R0lGODlhDAAMAIQAAP//9/X
 17unp5WZmZgAAAOfn515eXv
 Pz7Y6OjuDg4J+fn5OTk6enp
 56enmleECcgggoBADs=

application specific tag: !something |
 The semantics of the tag
 above may be different for
 different documents.
 

Global Tags

全局标记是URIs,而且可以使用句柄以标签简写表示。还可以使用特定于应用的本地标签。

%TAG ! tag:clarkevans.com,2002:
--- !shape
  # Use the ! handle for presenting
  # tag:clarkevans.com,2002:circle
- !circle
  center: &ORIGIN {x: 73, y: 129}
  radius: 7
- !line
  start: *ORIGIN
  finish: { x: 89, y: 102 }
- !label
  start: *ORIGIN
  color: 0xFFEEBB
  text: Pretty vector drawing.

Unordered Sets

# Sets are represented as a
# Mapping where each key is
# associated with a null value
--- !!set
? Mark McGwire
? Sammy Sosa
? Ken Griff

Ordered Mappings

# Ordered maps are represented as
# A sequence of mappings, with
# each mapping having one key
--- !!omap
- Mark McGwire: 65
- Sammy Sosa: 63
- Ken Griffy: 58

Full Length Example

两个完整示例,第一个是样品发票,第二个是日志文件示例

Invoice

--- !<tag:clarkevans.com,2002:invoice>
invoice: 34843
date   : 2001-01-23
bill-to: &id001
    given  : Chris
    family : Dumars
    address:
        lines: |
            458 Walkman Dr.
            Suite #292
        city    : Royal Oak
        state   : MI
        postal  : 48046
ship-to: *id001
product:
    - sku         : BL394D
      quantity    : 4
      description : Basketball
      price       : 450.00
    - sku         : BL4438H
      quantity    : 1
      description : Super Hoop
      price       : 2392.00
tax  : 251.42
total: 4443.52
comments:
    Late afternoon is best.
    Backup contact is Nancy
    Billsmer @ 338-4338.

Log File

---
Time: 2001-11-23 15:01:42 -5
User: ed
Warning:
  This is an error message
  for the log file
---
Time: 2001-11-23 15:02:31 -5
User: ed
Warning:
  A slightly different error
  message.
---
Date: 2001-11-23 15:03:17 -5
User: ed
Fatal:
  Unknown variable "bar"
Stack:
  - file: TopClass.py
    line: 23
    code: |
      x = MoreObject("345\n")
  - file: MoreClass.py
    line: 58
    code: |-
      foo = bar

qin

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码支持
扫码打赏

打开支付宝扫一扫,即可进行扫码打赏哦