Python yaml: Difference between revisions
From wikinotes
No edit summary |
No edit summary |
||
Line 5: | Line 5: | ||
<syntaxhighlight lang="python"> | <syntaxhighlight lang="python"> | ||
with open( filepath, 'r' ) as stream: | with open(filepath, 'r') as stream: | ||
my_dict = yaml. | my_dict = yaml.safe_load(stream) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 15: | Line 15: | ||
<blockquote> | <blockquote> | ||
<syntaxhighlight lang="python"> | <syntaxhighlight lang="python"> | ||
with open( filepath, 'w' ) as fd: | with open(filepath, 'w') as fd: | ||
fd.write( | fd.write( | ||
yaml.safe_dump( dictionary, default_flow_style=False ) | yaml.safe_dump(dictionary, default_flow_style=False) | ||
) | ) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
</blockquote><!-- write --> | </blockquote><!-- write --> |
Latest revision as of 02:13, 18 May 2023
read
with open(filepath, 'r') as stream: my_dict = yaml.safe_load(stream)
write
with open(filepath, 'w') as fd: fd.write( yaml.safe_dump(dictionary, default_flow_style=False) )