1

I want to output the yaml null but instead the python dumper is outputting a blank space instead.

I am using ruamel.yaml

{key: None} 

Should output

key: null

instead it outputs

key:
Anthon
  • 69,918
  • 32
  • 186
  • 246
Mattreex
  • 189
  • 2
  • 17

1 Answers1

2

You should override the representer for None, with your own:

import sys
import ruamel.yaml


def my_represent_none(self, data):
    return self.represent_scalar(u'tag:yaml.org,2002:null', u'null')

yaml = ruamel.yaml.YAML()
yaml.representer.add_representer(type(None), my_represent_none)

data = {'key': None}
yaml.dump(data, sys.stdout)

which gives:

key: null
Anthon
  • 69,918
  • 32
  • 186
  • 246