Question

Let

d = {3: u'blah', u'b': u'foo', (12,2,3) : u'bonjour'}

be a dictionary. If I use

g = yaml.dump(d)
h = yaml.load(g)
print g, h

it's very good, but I get lots of !!python/unicode everywhere, so that's not good for readability.

On the other hand, if I use :

g = yaml.safe_dump(d)
h = yaml.load(g)
print g, h

there are no-more the !!python/unicode but also not the !!python/tuple anymore, so the load doesn't work because the tuple isn't understood correctly.

How to get the right balance with YAML ?

  • no !!python/unicode everywhere,
  • having tuples as dict key working ! (so probably !!python/typle has to be kept)
Was it helpful?

Solution

If you search for python yaml unicode, the very first result is this bug report, which has exactly the solution you're looking for. In short, register a custom representer for unicode strings, like this:

yaml.add_representer(unicode, 
                     lambda dumper, value: dumper.represent_scalar(u'tag:yaml.org,2002:str', value))

With this in place, your example renders like this:

>>> d = {3: u'blah', u'b': u'foo', (12,2,3) : u'bonjour'}
>>> print yaml.dump(d)
3: blah
? !!python/tuple [12, 2, 3]
: bonjour
b: foo
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top