Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 362
Michael Delgado Avatar asked May 13 '16 02:05

Michael Delgado


1 Answers

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.

like image 75
Jace Browning Avatar answered Oct 12 '22 00:10

Jace Browning