How can I output blank value in python yaml file
Question: 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: If you load a YAML src

    
    
    allow:
    

into Python you get `None` assigned to the key `allow`, that is the correct
behaviour.

If you use [ruamel.yaml](https://pypi.python.org/pypi/ruamel.yaml) (of which I
am the author), and its `RoundTripDumper`, `None` is written as you want it
(which is IMO the most readable, although not explicit):

    
    
    import ruamel.yaml
    
    print ruamel.yaml.dump(dict(allow=None), Dumper=ruamel.yaml.RoundTripDumper)
    

will give you:

    
    
    allow:
    

You can also properly round-trip this:

    
    
    import ruamel.yaml
    
    yaml_src = """
    allow:
    key2: Hello  # some test
    
    """
    
    data = ruamel.yaml.load(yaml_src, ruamel.yaml.RoundTripLoader)
    print '#### 1'
    print data['allow']
    print '#### 2'
    print ruamel.yaml.dump(data, Dumper=ruamel.yaml.RoundTripDumper)
    
    print '#### 3'
    
    print type(data)
    

to get as output:

    
    
    #### 1
    None
    #### 2
    allow:
    key2: Hello  # some test
    
    #### 3
    

In the above, `data` is a subclass of ordereddict, which is necessary to keep
track of the flowstyle of the input, handling comments attached to lines,
order of the keys, etc..  
Such a subclass can be created on the fly, but it is normally easier to start
with some readable and well formatted YAML code (possible already saved on
disc) and then update/extend the values.

