How can I parse a YAML file in Python How can I parse a YAML file in Python python python

How can I parse a YAML file in Python


An easy method that does not rely on C headers is PyYaml (documentation), which can be installed via pip install pyyaml:

#!/usr/bin/env pythonimport yamlwith 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 to avoid introducing the possibility for arbitrary code execution yaml.safe_load() should always be preferred unless you explicitly need the arbitrary object serialized or deserialized.

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.


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

# -*- coding: utf-8 -*-import yamlimport io# Define datadata = {    'a list': [        1,         42,         3.141,         1337,         'help',         u'€'    ],    'a string': 'bla',    'another dict': {        'foo': 'bar',        'key': 'value',        'the answer': 42    }}# Write YAML filewith io.open('data.yaml', 'w', encoding='utf8') as outfile:    yaml.dump(data, outfile, default_flow_style=False, allow_unicode=True)# Read YAML filewith 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: blaanother dict:  foo: bar  key: value  the answer: 42

Common file endings

.yml and .yaml

Alternatives

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


If you have YAML that conforms to the YAML 1.2 specification (released 2009) then you should use ruamel.yaml (disclaimer: I am the author of that package).It is essentially a superset of PyYAML, which supports most of YAML 1.1 (from 2005).

If you want to be able to preserve your comments when round-tripping, you certainly should use ruamel.yaml.

Upgrading @Jon's example is easy:

import ruamel.yaml as yamlwith open("example.yaml") as stream:    try:        print(yaml.safe_load(stream))    except yaml.YAMLError as exc:        print(exc)

Use safe_load() unless you really have full control over the input, need it (seldom the case) and know what you are doing.

If you are using pathlib Path for manipulating files, you are better of using the new API ruamel.yaml provides:

from ruamel.yaml import YAMLfrom pathlib import Pathpath = Path('example.yaml')yaml = YAML(typ='safe')data = yaml.load(path)


matomo