A full-featured YAML processing framework for Python.
The documentation is available on the PyYAML website or in the form of GitHub flavoured markdown.
pip install pyyamlpip install git+https://github.com/yaml/pyyaml.git@masteror checkout the repository, or extract the .tar.gz and navigate inside and run:
python setup.py installBy default, the setup.py script checks whether LibYAML is installed and if
so, builds and installs LibYAML bindings.
When LibYAML bindings are installed, you may use fast C-based parser and emitter as follows:
yaml.load(stream, Loader=yaml.CLoader)
yaml.dump(data, Dumper=yaml.CDumper)python setup.py --with-libyaml installpython setup.py --without-libyaml installPyYAML includes a comprehensive test suite. To run the tests:
python setup.py testYour default and safest choice for YAML streams.
WARNING: CVE reference, etc. from wiki page.
Can load Python objects:
e.g. SafeLoader etc ...
Mention that these are the keyword arguments of the Dumper class and subclasses.
- the python object to serialise
Applies to safe_dump(), safe_dump_all() dump(), dump_all functions
and the Dumper class
- IOObject - anything with a
.write(), will be written to None- returns a string.
Type[Dumper]class/subclass ofDumper...
Optional[bool]True- what true doesFalse- what false doesNone- what None does
Mention that these are the keyword arguments of the Loader class and subclasses.
- YAML Spec 1.1 - Mostly supported.
- YAML Spec 1.2 - Partial Support.
- YAML Spec 1.3 - Not Supported Yet.
Why does
import yaml
document = """
a: 1
b:
c: 3
d: 4
"""
print(yaml.dump(yaml.load(document)))give
a: 1
b: {c: 3, d: 4}(see #18, #24)?
It's a correct output despite the fact that the style of the nested mapping is different.
By default, PyYAML chooses the style of a collection depending on whether it has nested collections. If a collection has nested collections, it will be assigned the block style. Otherwise it will have the flow style.
If you want collections to be always serialized in the block style, set the
parameter default_flow_style of dump() to False.
For instance,
>>> print(yaml.dump(yaml.load(document), default_flow_style=False))
a: 1
b:
c: 3
d: 4Starting from the 3.08 release, PyYAML and LibYAML bindings provide a complete support for Python 3. Python 2 support was dropped in the 6.0 release. This is a short outline of differences in PyYAML API between Python 2 and Python 3 versions.
In Python 2:
strobjects are converted into!!str,!!python/stror!binarynodes depending on whether the object is an ASCII, UTF-8 or binary string.unicodeobjects are converted into!!python/unicodeor!!strnodes depending on whether the object is an ASCII string or not.yaml.dump(data)produces the document as a UTF-8 encodedstrobject.yaml.dump(data, encoding=('utf-8'|'utf-16-be'|'utf-16-le'))produces astrobject in the specified encoding.yaml.dump(data, encoding=None)produces aunicodeobject.
In Python 3:
strobjects are converted to!!strnodes.bytesobjects are converted to!!binarynodes.- For compatibility reasons,
!!python/strand!python/unicodetags are still supported and the corresponding nodes are converted tostrobjects. yaml.dump(data)produces the document as astrobject.yaml.dump(data, encoding=('utf-8'|'utf-16-be'|'utf-16-le'))produces abytesobject in the specified encoding.
-
For more information, check the PyYAML homepage.
-
Discuss PyYAML with the maintainers on Matrix at https://matrix.to/#/#pyyaml:yaml.io or IRC #pyyaml irc.libera.chat
-
Submit bug reports and feature requests to the PyYAML bug tracker.
The PyYAML module was written by Kirill Simonov [email protected]. It is currently maintained by the YAML and Python communities.
PyYAML is released under the MIT license.
See the file LICENSE for more details.