How can I output blank value in python yaml file

user3214546 picture user3214546 · May 9, 2015 · Viewed 24k times · Source

I am writing yaml file like this

with open(fname, "w") as f:
     yaml.safe_dump({'allow':'', 'deny': ''}, f,
                    default_flow_style=False, width=50, indent=4)

This outputs:

allow: ''

I want to output as

allow:

How can I do that?

Answer

Koo picture Koo · Dec 20, 2018

For None type to be read from python use null in yaml

A YAML file test.yml like this

foo: null
bar: null

will be read by python as

import yaml
test = yaml.load(open('./test.yml'))
print(test)

foo: None
bar: None