Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I parse a YAML file in Python

Tags:

python

yaml

How can I parse a YAML file in Python?

like image 994
Szymon Lipiński Avatar asked Nov 20 '09 23:11

Szymon Lipiński


People also ask

How do 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.

Does Python have a built in YAML parser?

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.

How do I read and update a YAML file in Python?

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.


2 Answers

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

like image 114
Jon Avatar answered Oct 04 '22 13:10

Jon


Read & Write YAML files with Python 2+3 (and unicode)

# -*- 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) 

Created YAML file

a list: - 1 - 42 - 3.141 - 1337 - help - € a string: bla another dict:   foo: bar   key: value   the answer: 42 

Common file endings

.yml and .yaml

Alternatives

  • CSV: Super simple format (read & write)
  • JSON: Nice for writing human-readable data; VERY commonly used (read & write)
  • YAML: YAML is a superset of JSON, but easier to read (read & write, comparison of JSON and YAML)
  • pickle: A Python serialization format (read & write)
  • MessagePack (Python package): More compact representation (read & write)
  • HDF5 (Python package): Nice for matrices (read & write)
  • XML: exists too *sigh* (read & write)

For your application, the following might be important:

  • Support by other programming languages
  • Reading / writing performance
  • Compactness (file size)

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

like image 35
Martin Thoma Avatar answered Oct 04 '22 11:10

Martin Thoma