Leo E
Leo E

Reputation: 829

JSON to YAML in Python : How to get correct string manipulation?

I'm using the PyYAML library to convert json to yaml. But quotation marks are removed from string values when they should not be. They should only be removed from keys.

I have used the default_flow_style option with the yaml dump function as illustrated below :

import yaml

x = {'name':'Alice','location':'New York', 'profession':'coder'}
yaml.dump(x, default_flow_style=False)

The actual result is :
'location: New York\nname: Alice\nprofession: coder\n'

When the desired result is :
"location: 'New York'\nname: 'Alice'\nprofession: 'coder'\n"

Upvotes: 2

Views: 5866

Answers (1)

Anthon
Anthon

Reputation: 76598

The PyYAML library that you are using doesn't readily give you that kind of control. It drops the original quotation if superfluous when you load-then-dump YAML (version 1.1) with it. And if you specify default_style='"' every string gets quoted, including keys.

ruamel.yaml (disclaimer: I am the author of that package) can do this kind of round-tripping while allowing you to retain your specific and/or superfluous quotes. Its mechanism to do so, provides you with types that you can use out of the box.

Thanks to David Fraser you can also easily have non-quoted mapping keys, while all the rest is double quoted:

import sys
import ruamel.yaml


def non_quoted_key(self, data):
   if isinstance(data, ruamel.yaml.compat.string_types):
       data = ruamel.yaml.scalarstring.PlainScalarString(data)
   return self.represent_data(data)

x = {'name':'Alice', 'location':'New York', 'profession':'coder'}
yaml = ruamel.yaml.YAML()
yaml.default_flow_style = False
yaml.default_style = '"'
yaml.Representer.represent_key = non_quoted_key
yaml.dump(x, sys.stdout)

which gives:

name: "Alice"
location: "New York"
profession: "coder"

Upvotes: 2

Related Questions