Python dump YAML uses double quotes around the string

Python dump YAML uses double quotes around the string … here is a solution to the problem.

Python dump YAML uses double quotes around the string

In Python 3.5, I dumped the following dictionary into a .yaml file.

D={'name': 'mydata', value: {'x': 1, 'y': 2, 'z':3}}

When I run the following code:

import ruamel
import ruamel.yaml as yaml
D={'name': 'mydata', 'value': {'x': 1, 'y': 2, 'z':3}}
yaml.round_trip_dump(D, open('my_yaml.yaml', 'w'),
                     default_flow_style=False, indent=4)

The resulting my_yaml.yaml looks like this:

name: mydata                                                                    
value:                                                                     
    z: 3                                                                     
    x: 1                                                                   
    y: 2

My question is, is there a convenient way to write double quotes around mydata, i.e., instead of name: mydata, which is written as name:my data"

Solution

There is a relatively simple way to do this:

import sys
import ruamel.yaml

S = ruamel.yaml.scalarstring.DoubleQuotedScalarString
D = {'name': S('mydata'), 'value': {'x': 1, 'y': 2, 'z':3}}

yaml = ruamel.yaml.YAML()
yaml.indent(mapping=4)
yaml.dump(D, sys.stdout)

The last three lines, using YAML instances, are a newer practice:

ruamel.yaml.round_trip_dump(D, sys.stdout, indent=4)

With the new API, you can specify different indentation values for sequences.

Either way, the above provides you with:

name: "mydata"
value:
    x: 1
    y: 2
    z: 3

When using ruamel.yaml's round-trip pattern, you don’t need to explicitly execute default_flow_style=False.


DoubleQuotedScalarString is a subclass of str that stores “mydata" if you load your preferred output while keeping the reference:

yaml.preserve_quotes = True
data = yaml.load("""\
name: "mydata"
value:
    x: 1
    y: 2
    z: 3
""")
print(type(data['name']))

Give:

<class 'ruamel.yaml.scalarstring.DoubleQuotedScalarString'>

If your output round trips are correct, it’s always a good idea to check the data structure ruamel.yaml loading, whether it’s double quotes, block-style literal scalars, hexadecimal integers, or comment saving.
The library does a lot of work behind the scenes without documentation, and the authors of this library may not bother to offer more.

Related Problems and Solutions