YAML (YAML Ain't Markup Language) is an indentation-based file format for data serialization. A JSON (JavaScript Object Notation) doc can be easily converted to a YAML doc, since JSON is a subset of YAML.
NOTE: Only spaces is allowed for indentation, not tabs.
YAML | JSON |
---|---|
# This is a comment
number: 3.14159
bool: true
string: 'hello'
another string: bye bye
dict:
name: Simon
weight: 66
# the style below is called flow style, whereas the one above block style
another-dict: {name: Simon, weight: 66} |
{
"number": 3.14159,
"bool": true,
"string": "hello",
"another string": "bye bye",
"dict": {
"name": "Simon",
"weight": 66
},
"another-dict": {
"name": "Simon",
"weight": 66
}
} |
YAML | JSON |
---|---|
list:
- apple
- pear
- orange
another_list: [apple, pear, orange]
'list of dict':
- name: Simon
weight: 66
- surname: Zhao |
{
"list": [
"apple",
"pear",
"orange"
],
"another_list": [
"apple",
"pear",
"orange"
],
"list-of-dict": [
{
"name": "Simon",
"weight": 66
},
"surname": "Zhao"
]
} |
YAML | JSON |
---|---|
code: |
if args.version:
print(version) |
{
"code": "if args.version:\n print(version)\n"
} |
YAML | JSON |
---|---|
---
name: Simon
---
name: Zhao |
{"name": "Simon"}
{"name": "Zhao"} |
The pyyaml package can be used to deal with YAML file:
pip install pyyaml
To read YAML file:
import yaml
# read YAML string
doc = """
a: 1
b:
c: 3
d: 4
"""
print(yaml.load(doc))
# read YAML file containing a single YAML document
with open('xxx.yaml') as f:
print(yaml.load(f))
# read YAML file containing multiple YAML documents
with open('yyy.yaml') as f:
for doc in yaml.load_all(f)
print(doc)
# read YAML file and keep the order of keys as well.
# the returned object is an OrderedDict
import yamlordereddictloader # pip install yamlordereddictloader
with open('xxx.yaml') as f:
print(yaml.load(f, Loader=yamlordereddictloader.Loader))
To convert Python data structure to YAML format:
import yaml
one = {'name': 'Simon', 'weight': 66}
two = [one, one]
# dump one object
print(yaml.dump(one))
# dump multiple objects
print(yaml.dump_all(two, , explicit_start=True))