Can I dump blank instead of null in yaml/pyyaml?

Michael Delgado picture Michael Delgado · May 13, 2016 · Viewed 7.2k times · Source

Using PyYAML, if I read in a file with blank values in a dict:

test_str = '''
attrs:
  first:
  second: value2
'''

This returns None for the key first:

>>> data = yaml.load(test_str)
>>> data
{'attrs': {'second': 'value2', 'first': None}}

But when writing, the None value is replaced with null:

>>> print(yaml.dump(data, default_flow_style=False))
attrs:
  first: null
  second: value2

Is there a way to format the dump output to print a blank scalar rather than null?

Answer

Jace Browning picture Jace Browning · Jan 22, 2017

Based on @Anthon's excellent answer, I was able to craft this solution:

def represent_none(self, _):
    return self.represent_scalar('tag:yaml.org,2002:null', '')

yaml.add_representer(type(None), represent_none)

Based on my understanding of the PyYAML code, adding a representer for an existing type should simply replace the existing representer.

This is a global change and that means that all following dumps use a blank. If some unrelated other piece of code in your program relies on None to be represented in the "normal" way, e.g. a library that you import and that uses PyYAML as well, that library will no longer work in the exepected way/correctly, in that case subclassing is the correct way to go.