Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading empty dictionary when YAML file is empty (Python 3.4)

I have a YAML file that is empty, and when I load it in, I would like to load it in as an empty dictionary. For example, I have

import yaml
with open('an_empty_file.yml', 'r') as config_file:
    config=yaml.load(config_file)
    print(config) 
None

It turns out that yaml.load(config_file) will return a NoneType object, which I suppose makes sense. Is there an easy way to just return an empty dictionary?

like image 840
Vincent Avatar asked Feb 29 '16 02:02

Vincent


People also ask

How do you assign an empty dictionary in Python?

To create an empty dictionary, first create a variable name which will be the name of the dictionary. Then, assign the variable to an empty set of curly braces, {} . Another way of creating an empty dictionary is to use the dict() function without passing any arguments.

Is Empty file valid YAML?

An empty YAML file is a valid YAML file with a scalar that is NULL by absence of any specific value.

How do I pass none in YAML?

In YAML a blank value is interpreted as null, so whats the differance? Just use None . It will get interpreted the same way. Unless, of course, there is some very specific reason you need it to be blank regardless of how it is interpreted.

How do you check a dictionary is empty or not in Python?

# Checking if a dictionary is empty by checking its length empty_dict = {} if len(empty_dict) == 0: print('This dictionary is empty! ') else: print('This dictionary is not empty! ') # Returns: This dictionary is empty!


2 Answers

If it returns None, you can just use or so your config will hold an empty dictionary by default if yaml.load returns None:

config = yaml.load(config_file) or {}

Ultimately, what is happening here, starts from the left hand side:

We are right now assigning a value in to config. We are stating to assign yaml.load(config_file) in to config, however, by using the or, what we are saying in this statement is that, if it (yaml.load) evaluates to a None or False condition (i.e. In this case for us, None), we will then assign {} to config.

Quick demo taking an empty string as config_file:

>>> import yaml
>>> config_file = ''
>>> config = yaml.load(config_file) or {}
>>> print(config)
{}
like image 68
idjaw Avatar answered Sep 19 '22 17:09

idjaw


At the top level a YAML file can have

  • a mapping, indicated by key value pairs separated by : (optionally in flow style using {}),
  • a sequence indicated by - if it is block style and [ ] if it is flow style
  • a (single) scalar.

Your file is not a mapping or a sequence, so it is a scalar and since the scalar with an empty representation is considered to be the same as specifying

null

in the file.

To load this as an empty dictionary and not as None you can do:

with open('an_empty_file.yml', 'r') as config_file:
    config = yaml.load(config_file)
    config = {} if config is None else config
    print(config)

You should never try and take a short cut of doing:

config = yaml.load(config_file) or {}

as this would also cause files with the single scalar 0:

0

with single scalar floats 0.0:

0.0

with hex scalars 0x0:

0x0

with an empty double quoted scalar ""

""

with an empty single quoted scalar ''

''

with an empty folded scalar:

>

with an empty literal style scalar:

|

with the single boolean False:

False

or no ¹:

no

or off ¹:

off

as well as the empty sequence:

[

]

to result in config to be an empty dictionary.

The number of different file contents that would be incorrectly changed to empty dictionaries is endless.


¹ This is a result of PyYAML never been updated for 1.1 to the 1.2 standard published in 2009. If it would be it would also convert octals of the form 0o0.

like image 34
Anthon Avatar answered Sep 23 '22 17:09

Anthon