Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I output blank value in python yaml file

Tags:

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?

like image 404
user3214546 Avatar asked May 08 '15 23:05

user3214546


People also ask

How do you give an empty value in YAML?

Empty values: null can be used to represent an empty value, such as an empty list or an empty string. Placeholder values: null can be used as a placeholder value when the actual value is not yet known.

Can YAML have null?

Nulls in YAML can be expressed with null or ~ .

Is an empty string valid YAML?

As you can see, since both l-document-prefix and l-any-document are optional, an empty YAML stream is valid – but contains no document. If you're asking whether l-any-document can produce the empty string, the answer is no.


1 Answers

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 
like image 159
Koo Avatar answered Oct 21 '22 22:10

Koo