Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding quotes using ruamel.yaml

In the https://pypi.python.org/pypi/ruamel.yaml change log there is an entry: 0.11.12 (2016-07-06):

- added support for roundtrip of single/double quoted scalars using:
  ruamel.yaml.round_trip_load(stream, preserve_quotes=True)

Using ruamel I can convert:

skip: Skip
show: 'Show'
remove: "Remove"
"info_on": "ON"

to:

skip: Skip
show: Show
remove: Remove
info_on: ON

Is there an option in ruamel to add rather than preserve quotes resulting in:

"skip": "Skip"
"show": "Show"
"remove": "Remove"
"info_on": "ON"
like image 737
Ian Avatar asked Feb 10 '26 00:02

Ian


1 Answers

You can do that with the dump() option default_style='"':

import sys
import ruamel.yaml

yaml_str = """\
skip: Skip
show: 'Show'
remove: "Remove"
"info_on": "ON"
"""

data = ruamel.yaml.round_trip_load(yaml_str)

ruamel.yaml.round_trip_dump(data, sys.stdout, default_style='"')

in the same way as you can do for the old PyYAML from which ruamel.yaml is derived.

like image 82
Anthon Avatar answered Feb 12 '26 16:02

Anthon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!