YAML in Python: Difference between revisions
Jump to navigation
Jump to search
(→PyYAML) |
|||
Line 9: | Line 9: | ||
<font color=darkkhaki>To install, see the "Installation" section from https://pyyaml.org/wiki/PyYAMLDocumentation.</font> | <font color=darkkhaki>To install, see the "Installation" section from https://pyyaml.org/wiki/PyYAMLDocumentation.</font> | ||
=Deserialize YAML= | |||
<syntaxhighlight lang='python'> | <syntaxhighlight lang='python'> | ||
import yaml | import yaml | ||
Line 18: | Line 18: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
=Serialize YAML= | |||
<syntaxhighlight lang='python'> | <syntaxhighlight lang='python'> | ||
import yaml | import yaml | ||
Line 25: | Line 25: | ||
print (yaml.dump(data, default_flow_style=False)) | print (yaml.dump(data, default_flow_style=False)) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
=Safely Navigate a Complex Data Structure= | |||
Suggestions on how to safely recursively navigate a complex data structure: | Suggestions on how to safely recursively navigate a complex data structure: | ||
{{Internal|Python Safely Navigate a Complex Data Structure#Overview|Safely Navigate a Complex Data Structure}} | {{Internal|Python Safely Navigate a Complex Data Structure#Overview|Safely Navigate a Complex Data Structure}} |
Revision as of 00:10, 3 March 2022
Internal
PyYAML
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))
Suggestions on how to safely recursively navigate a complex data structure: