1# @Time    : 2019-02-02
2# @Language: Markdown
3# @Software: VS Code
4# @Author  : Di Wang
5# @Email   : [email protected]

A Short Report on YAML

YAML means “YAML Ain’t a Markup Language”. It uses Python-style indentation to indicate nesting, and use [] for lists and {} for maps.

Since the name is recursive, it is a better option to explain YAML by YAML.

 1--- # Syntax
 2basic syntax:
 3  - whitespace indentation is used for denoteing structure and `tab` is not allowed
 4  - comments begin with the `#`
 5  - List members are denoted by leading hyphen (`-`) with one member per line
 6  - use key:value to denote array
 7  - case sensitive
 8Support data structure:
 9  - map (also called dictionary or hashes or key:value pair)
10  - array or list or sequence
11  - scalars
12
13# basic components
14map:
15  - map1: this is a map
16  - map2: {name: hello, id: world}
17list:
18    - cat
19    - dog
20    - cow
21    - [monkey, elephant]
22scalars:
23  null: ~
24  boolean:
25    - TRUE # True (yaml1.1), string "Yes" (yaml1.2)
26    - FALSE
27    - yes
28    - no
29    - Yes
30    - YeS # will be recognized as a string
31  float:
32    - 3.14
33    - 2.99e+8
34  int:
35    - 65536
36    - 0b1010 # dec: 10
37  srting:
38    - apple
39    - "another string"
40    - '\n will be escaped'
41    - "\n will not be escaped"
42    - data: |
43        There once was a tall man from Ealing
44        Who got on a bus to Darjeeling
45          It said on the door
46          "Please don't sit on the floor"
47        So he carefully sat on the ceiling        
48    - info: >
49        this is
50        an
51        info
52
53        \n
54        continued
55        info        
56date and time:
57  # {date}t{time}+timezone
58  - iso8601: 2001-12-25t01:23:45.54+09:00
59  - date: &epoch 1970-01-01
60  - epochtime: *epoch
61explicit type:
62  str_true: !!str true # string rather than boolean
63  str_int: !!str 123 # string rather than int
64anchor and refers:
65  - anchor: &anchor001
66      Manager: Alice
67      Programer: Bob
68      Teacher: Dylan
69      Student: Ruby
70  - refer: *anchor001
71  - merger:
72      <<: *anchor001
73      Student: Python

Parse this YAML file by Python3.

1import yaml
2import pprint
3
4f = open("./test.yaml")
5a = yaml.load(f)
6print(type(a))
7pprint.pprint(a)

This is the output. It seems there are some errors about the highlight color.

 1<class 'dict'>
 2{'Support data structure': ['map (also called dictory or hashes or key:value '
 3                            'pair)',
 4                            'array or list or sequence',
 5                            'scalars'],
 6 'anchor and refers': [{'anchor': {'Manager': 'Alice',
 7                                   'Programer': 'Bob',
 8                                   'Student': 'Ruby',
 9                                   'Teacher': 'Dylan'}},
10                       {'refer': {'Manager': 'Alice',
11                                  'Programer': 'Bob',
12                                  'Student': 'Ruby',
13                                  'Teacher': 'Dylan'}},
14                       {'merger': {'Manager': 'Alice',
15                                   'Programer': 'Bob',
16                                   'Student': 'Python',
17                                   'Teacher': 'Dylan'}}],
18 'basic syntax': ['whitespace indentation is used for denoteing structure and '
19                  '`tab` is not allowed',
20                  'comments begin with the `#`',
21                  'List members are denoted by leading hyphen (`-`) with one '
22                  'member per line',
23                  'use key:value to denote array',
24                  'case sensitive'],
25 'date and time': [{'iso8601': datetime.datetime(2001, 12, 24, 16, 23, 45, 540000)},
26                   {'date': datetime.date(1970, 1, 1)},
27                   {'epochtime': datetime.date(1970, 1, 1)}],
28 'explicit type': {'str_int': '123', 'str_true': 'true'},
29 'list': ['cat', 'dog', 'cow', ['monkey', 'elephant']],
30 'map': [{'map1': 'this is a map'}, {'map2': {'id': 'world', 'name': 'hello'}}],
31 'scalars': {None: None,
32             'boolean': [True, False, True, False, True, 'YeS'],
33             'float': [3.14, 299000000.0],
34             'int': [65536, 10],
35             'srting': ['apple',
36                        'another string',
37                        '\\n will be escaped',
38                        '\n will not be escaped',
39                        {'data': 'There once was a tall man from Ealing\n'
40                                 'Who got on a bus to Darjeeling\n'
41                                 '  It said on the door\n'
42                                 '  "Please don\'t sit on the floor"\n'
43                                 'So he carefully sat on the ceiling\n'},
44                        {'info': 'this is an info\n\\n continued info\n'}]}}