-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyaml
37 lines (27 loc) · 909 Bytes
/
yaml
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
The top of a yaml formatted file should have three hyphens at the top. Python doesn’t do this automatically
# pip install pyyaml
import yaml
To convert a list to yaml using standard version:
yaml.dump(my_list)
To convert a list to yaml using the condensed version:
yaml.dump(my_list, default_flow_style=True)
To convert a list to the more human readable format:
yaml.dump(my_list, default_flow_style=False)
To write yaml to a file:
with open("my_file.yml", "w") as f:
f.write(yaml.dump(my_list, default_flow_style=False))
To read in a yaml file:
with open("my_file.yml", "r") as f:
new_list = yaml.load(f)
### YAML file format rules ###
In yaml file starts with:
---
# You can comment a yaml file with # sign
# a list begins with a hypen
- element1
- element2
- element3
# You can do a dictionary like this
- key1: value1
# or dictionary like this
{key1: value1, key2:value2}