The Python Oracle

How can I parse a YAML file in Python

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Mysterious Puzzle

--

Chapters
00:00 Question
00:17 Accepted answer (Score 1305)
01:25 Answer 2 (Score 205)
03:45 Answer 3 (Score 85)
04:49 Answer 4 (Score 58)
05:17 Thank you

--

Full question
https://stackoverflow.com/questions/1773...

Accepted answer links:
[documentation]: http://pyyaml.org/wiki/PyYAMLDocumentati...
[YAML 1.1 specification]: https://yaml.org/spec/1.1/spec.html
[YAML 1.2 specification]: https://yaml.org/spec/1.2/spec.html
[ruamel.yaml]: https://yaml.readthedocs.io/
[this answer]: https://stackoverflow.com/a/38922434/313...
[oyaml]: https://github.com/wimglenn/oyaml
[synk of oyaml here]: https://snyk.io/advisor/python/oyaml

Answer 2 links:
[read & write]: https://stackoverflow.com/a/41585079/562...
[read & write]: https://stackoverflow.com/a/37795053/562...
[read & write]: https://stackoverflow.com/a/42054860/562...
[comparison of JSON and YAML]: https://stackoverflow.com/a/1729545/5627...
[read & write]: https://stackoverflow.com/a/33245595/562...
[MessagePack]: http://msgpack.org/
[Python package]: https://pypi.python.org/pypi/msgpack-pyt...
[read & write]: https://stackoverflow.com/q/43442194/562...
[HDF5]: https://en.wikipedia.org/wiki/Hierarchic...
[Python package]: http://docs.h5py.org/en/latest/quick.htm...
[read & write]: https://stackoverflow.com/a/41586571/562...
[read]: https://stackoverflow.com/a/1912483/5627...
[write]: https://stackoverflow.com/a/3605831/5627...
[Comparison of data serialization formats]: https://en.wikipedia.org/wiki/Comparison...
[Configuration files in Python]: https://martin-thoma.com/configuration-f.../

Answer 3 links:
[YAML 1.2 specification]: http://www.yaml.org/spec/1.2/spec.html
[ruamel.yaml]: https://pypi.python.org/pypi/ruamel.yaml/

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #yaml

#avk47



ACCEPTED ANSWER

Score 1464


The easiest and purest method without relying on C headers is PyYaml (documentation), which can be installed via pip install pyyaml:

import yaml

with open("example.yaml") 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 to avoid introducing the possibility for arbitrary code execution. So unless you explicitly need the arbitrary object serialization/deserialization use safe_load.

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




ANSWER 2

Score 237


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

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




ANSWER 3

Score 88


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 yaml

with 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 YAML
from pathlib import Path

path = Path('example.yaml')
yaml = YAML(typ='safe')
data = yaml.load(path)



ANSWER 4

Score 64


First install pyyaml using pip3.

Then import yaml module and load the file into a dictionary called 'my_dict':

import yaml
with open('filename.yaml') as f:
    my_dict = yaml.safe_load(f)

That's all you need. Now the entire yaml file is in 'my_dict' dictionary.