I have YAML file site.yaml
:
Kvm_BLOCK:
ip_address: 10.X.X.X
property: null
server_type: zone
loaded and then dumped with:
ruamel.yaml.dump(site_yaml, new_file, Dumper=ruamel.yaml.RoundTripDumper)
it becomes
Kvm_BLOCK:
ip_address: 10.X.X.X
property:
server_type: zone
how to retain this null
value in property block
The
null
value in YAML 1.2 (constructed as Python'sNone
) can be represented asnull
,Null
,NULL
and~
, as specified here.Additionally:
Therefore your
null
value is not gone, it is just represented differently by the default representation fornull
inruamel.yaml
when usingRoundTripDump
. If you load that output again, you once more get aNone
as value for the keyproperty
If that is not to your liking you can change the output for all
None
/null
values by doing:which will dump:
You can get finer grained control by creating different classes in Python (
NULL
,Null
,null
, etc. ) and have different representers for each of them (much in the same way that thestring
subclasses inruamel.yaml.scalarstring.py
are used to represent a string in different ways (double quoted, single quoted, literal block style scalar). The problem is that you cannot subclassNoneType
so this is not so easily done transparently as with the string scalars.