YAML in Python: Difference between revisions
Jump to navigation
Jump to search
(→PyYAML) |
|||
Line 27: | Line 27: | ||
with open('some-file.yaml', 'rt') as f: | with open('some-file.yaml', 'rt') as f: | ||
content = f.read() | content = f.read() | ||
data = yaml.load(content, Loader=yaml.Loader) | #data = yaml.load(content, Loader=yaml.Loader) | ||
data = yaml.load(content) | |||
</syntaxhighlight> | </syntaxhighlight> | ||
Revision as of 20:41, 8 July 2022
Internal
PyYAML
PyYAML provides YAML serialization/deserialization in Python.
Installation
pip install pyyaml
requirements.txt
:
pyyaml == 5.3.1
To install, see the "Installation" section from https://pyyaml.org/wiki/PyYAMLDocumentation.
Deserialize YAML
import yaml
with open('some-file.yaml', 'rt') as f:
content = f.read()
#data = yaml.load(content, Loader=yaml.Loader)
data = yaml.load(content)
Serialize YAML
import yaml
# with the default flow style, the document is rendered in a non-indented manner
print (yaml.dump(data, default_flow_style=False))
Suggestions on how to safely recursively navigate a complex data structure: