How can I parse a YAML file in Python?
We can read the YAML file using the PyYAML module's yaml. load() function. This function parse and converts a YAML object to a Python dictionary ( dict object). This process is known as Deserializing YAML into a Python.
However, Python lacks built-in support for the YAML data format, commonly used for configuration and serialization, despite clear similarities between the two languages. In this tutorial, you'll learn how to work with YAML in Python using the available third-party libraries, with a focus on PyYAML.
The yaml file should be parsed and updated as below. How do I parse the values and update them appropriately? If you use PyYaml, you can use Loader to load data, and Dumper to write data to file. The data loaded is an ordinary dictionary in Python so you can access element by key and thus change it as you wish.
The easiest and purest method without relying on C headers is PyYaml (documentation), which can be installed via pip install pyyaml
:
#!/usr/bin/env python import yaml with open("example.yaml", "r") as stream: try: print(yaml.safe_load(stream)) except yaml.YAMLError as exc: print(exc)
And that's it. A plain yaml.load()
function also exists, but yaml.safe_load()
should always be preferred unless you explicitly need the arbitrary object serialization/deserialization provided in order to avoid introducing the possibility for arbitrary code execution.
Note the PyYaml project supports versions up through the YAML 1.1 specification. If YAML 1.2 specification support is needed, see ruamel.yaml as noted in this answer.
Also, you could also use a drop in replacement for pyyaml, that keeps your yaml file ordered the same way you had it, called oyaml. View synk of oyaml here
# -*- coding: utf-8 -*- import yaml import io # Define data data = { 'a list': [ 1, 42, 3.141, 1337, 'help', u'€' ], 'a string': 'bla', 'another dict': { 'foo': 'bar', 'key': 'value', 'the answer': 42 } } # Write YAML file with io.open('data.yaml', 'w', encoding='utf8') as outfile: yaml.dump(data, outfile, default_flow_style=False, allow_unicode=True) # Read YAML file with open("data.yaml", 'r') as stream: data_loaded = yaml.safe_load(stream) print(data == data_loaded)
a list: - 1 - 42 - 3.141 - 1337 - help - € a string: bla another dict: foo: bar key: value the answer: 42
.yml
and .yaml
For your application, the following might be important:
See also: Comparison of data serialization formats
In case you are rather looking for a way to make configuration files, you might want to read my short article Configuration files in Python
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With