π Edit page
β Add page
YAML
YAML is a human-friendly data serialization language for all programming languages. YAML is most often used for configuration files, but itβs also used for data exchange
Install package
Use the PyYAML package.
$ pip install PyYAML
Read
From file
import yaml
with open(file_path, "r") as f:
data = yaml.safe_load(f)
print(data)
Access the data as dictionary of dictionaries and lists. e.g.
data['abc'][0]
data['def']['x']
From string
contents = """
abc:
d: 1000
e: 10
"""
data = yaml.safe_load(contents)
Write
import yaml
output = {
'abc': [100, 10]
}
with open('out_file.yaml', 'w') as f:
yaml.dump(data, f)