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

yaml null
yaml dump
ruamel-yaml
pyyaml anchors
yaml representer
pyyaml tags
pyyaml flow style
yaml add_implicit_resolver

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?


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.

How can I output blank value in python yaml file, import ruamel.yaml print ruamel.yaml.dump(dict(allow=None), There is a way to do this built into python yaml itself. foo: null bar: null. will  When using indentation this seems to be applied to the value of the list instead of to the list itself, as you can see below indent=4 is applied after the leading - and not to the list itself. >


You get null because dump() uses the Representer() which subclasses SafeRepresenter() and to represent None, the following method is called:

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

As the string null is hardcoded, there is no option to dump() to change that.

The proper way to solve this in PyYAML is to make your own Dumper subclass which has the Emitter, Serializer, and Resolver from the standard Dumper that dump() uses, but with subclass of Representer that represents None the way you want it:

import sys
import yaml

from yaml.representer import Representer
from yaml.dumper import Dumper
from yaml.emitter import Emitter
from yaml.serializer import Serializer
from yaml.resolver import Resolver


yaml_str = """\
attrs:
  first:
  second: value2
"""

class MyRepresenter(Representer):
    def represent_none(self, data):
        return self.represent_scalar(u'tag:yaml.org,2002:null',
                                 u'')

class MyDumper(Emitter, Serializer, MyRepresenter, Resolver):
    def __init__(self, stream,
            default_style=None, default_flow_style=None,
            canonical=None, indent=None, width=None,
            allow_unicode=None, line_break=None,
            encoding=None, explicit_start=None, explicit_end=None,
            version=None, tags=None):
        Emitter.__init__(self, stream, canonical=canonical,
                indent=indent, width=width,
                allow_unicode=allow_unicode, line_break=line_break)
        Serializer.__init__(self, encoding=encoding,
                explicit_start=explicit_start, explicit_end=explicit_end,
                version=version, tags=tags)
        MyRepresenter.__init__(self, default_style=default_style,
                default_flow_style=default_flow_style)
        Resolver.__init__(self)

MyRepresenter.add_representer(type(None),
                              MyRepresenter.represent_none)

data = yaml.load(yaml_str)
yaml.dump(data, stream=sys.stdout, Dumper=MyDumper, default_flow_style=False)

gives you:

attrs:
  first:
  second: value2

If that sounds like a lot of overhead just to get rid of null, it is. There are some shortcuts you can take and you can even try to graft the alternate function onto the existing Representer, but since the actual function taken is referenced in a lookup table ( populated by add_representer ) you need to handle at least that reference as well.

The far more easy solution is replace PyYAML with ruamel.yaml and use its round_trip functionality (disclaimer: I am the author of that package):

import ruamel.yaml

yaml_str = """\
# trying to round-trip preserve empty scalar
attrs:
  first:
  second: value2
"""

data = ruamel.yaml.round_trip_load(yaml_str)
assert ruamel.yaml.round_trip_dump(data) == yaml_str

apart from emitting None as the empty scalar, it also preserves order in mapping keys, comments and tag names, none of which PyYAML does. ruamel.yaml also follows the YAML 1.2 specification (from 2009), where PyYAML uses the older YAML 1.1.


The ruamel.yaml package can be installed with pip from PyPI, or with modern Debian based distributions, also with apt-get python-ruamel.yaml

YAML loading and serializing does not handle None properly · Issue , Description of Issue/Question I am using import_yaml to load a YAML file that /​37200150/can-i-dump-blank-instead-of-null-in-yaml-pyyaml. PyYaml converts empty values to None when they are put in the yaml dictionary, and I am not sure how you would differentiate between having None or empty when dumping back to a file. I am going to mark this as a bug, but I would highly recommend using file.managed if you and values to remain empty instead of having None specified.


just use string replace

print(yaml.dump(data).replace("null", ""))

The YAML Format (The Yaml Component, Strings in YAML can be wrapped both in single and double quotes. When the string is null or ~ (otherwise, it would be considered as a null value);; When YAML can also use flow styles for collections, using explicit indicators rather than​  An empty array is always dumped as a YAML mapping. Sometimes you want it dumped as a YAML sequence instead. Commits ----- 87ffaf2 Bump version number af7067c Dump empty object as mapping a6d94c1 [Yaml] Allow dumping empty array as YAML sequence


pyaml · PyPI, PyYAML-based module to produce pretty and readable YAML-serialized data. Does not dump “null” values, if possible, replacing these with just empty Result for the (rather meaningless) example above (without any additional tweaks): PyYaml fails to load YAML from string when one of the parent = WorkingParent() yaml_parent_str = yaml.dump You can see that YAML is not able to create an


Null Language-Independent Type for YAML™ Version 1.1, This is typically converted into any native null-like value (e.g., undef in Perl, None in Python). Note that a null is different from an empty string  PyYAML is a YAML parser and emitter for the Python programming language. PyYAML features. a complete YAML 1.1 parser. In particular, PyYAML can parse all examples from the specification. The parsing algorithm is simple enough to be a reference for YAML parser implementors. Unicode support including UTF-8/UTF-16 input/output and *


Python yaml.safe_dump() Examples, This page provides Python code examples for yaml.safe_dump. Rather, you can save it to file and use the import-configuration command to apply it. '{foo: [​bar, {lol: true}], hello: 42}\\n' >>> yaml.dump(b, default_flow_style=True) '!bunch. data = source.read() new_data = yaml.safe_load(data) # If result is null, convert to  Fetching Unicode fixed that but that had the YAML side-effect of displaying !!python/unicode just about everywhere in the output YAML. 6. To fix the latter problem, it was suggested that we use yaml.safe_dump instead of yaml.dump, which I did (as you can see in the yamlutil.py referred to above).