YAML in Python: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 7: Line 7:
PyYAML provides YAML serialization/deserialization in Python.
PyYAML provides YAML serialization/deserialization in Python.


<font color=darkkhaki>To install, see
<font color=darkkhaki>To install, see the "Installation" section from https://pyyaml.org/wiki/PyYAMLDocumentation.</font>
 
==Deserialize YAML==
<syntaxhighlight lang='python'>
import yaml
 
with open('some-file.yaml', 'rt') as f:
  content = f.read()
data = yaml.load(content, Loader=yaml.Loader)
</syntaxhighlight>
 
==Serialize YAML==
<syntaxhighlight lang='python'>
import yaml
 
# with the default flow style, the document is rendered in a non-indented manner
print (yaml.dump(data, default_flow_style=False))
</syntaxhighlight>

Revision as of 04:48, 16 February 2022

Internal

PyYAML

https://pyyaml.org/
https://pyyaml.org/wiki/PyYAMLDocumentation

PyYAML provides YAML serialization/deserialization in Python.

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)

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