Serializing YAML with PyYAML: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
(Created page with "=Internal= * YAML in Python =Overview=")
 
Line 2: Line 2:
* [[YAML_in_Python#Serialize_YAML|YAML in Python]]
* [[YAML_in_Python#Serialize_YAML|YAML in Python]]
=Overview=
=Overview=
The process of serialization to YAML is rendering an in-memory data structure as a YAML-formatted string. The simplest sequence of statements that does that is:
<syntaxhighlight lang='py'>
import yaml
data = {
    'color': 'red',
    'size': 10,
    'parts': ['top', 'middle', 'bottom']
}
yaml_string = yaml.dump(data)
</syntaxhighlight>
The YAML-formatted string will be:
<syntaxhighlight lang='yaml'>
color: red
parts:
- top
- middle
- bottom
size: 10
</syntaxhighlight>

Revision as of 22:54, 7 December 2022

Internal

Overview

The process of serialization to YAML is rendering an in-memory data structure as a YAML-formatted string. The simplest sequence of statements that does that is:

import yaml

data = {
    'color': 'red',
    'size': 10,
    'parts': ['top', 'middle', 'bottom']
}

yaml_string = yaml.dump(data)

The YAML-formatted string will be:

color: red
parts:
- top
- middle
- bottom
size: 10