简体   繁体   English

在YAML文件为空时加载空字典(Python 3.4)

[英]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. 我有一个空的YAML文件,当我加载它时,我想将其作为空字典加载。 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. 事实证明, yaml.load(config_file)将返回一个NoneType对象,我认为这是有道理的。 Is there an easy way to just return an empty dictionary? 有一种简单的方法可以返回一个空字典吗?

If it returns None , you can just use or so your config will hold an empty dictionary by default if yaml.load returns None: 如果它返回None ,你可以使用or如果yaml.load返回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. 我们现在正在为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 (ie In this case for us, None), we will then assign {} to config. 我们声明将yaml.load(config_file)分配给config,但是,通过使用or ,我们在这个语句中说的是,如果它( yaml.load )计算为None或False条件(即在此对我们来说,没有),然后我们将{}分配给配置。

Quick demo taking an empty string as config_file: 快速演示将空字符串作为config_file:

>>> import yaml
>>> config_file = ''
>>> config = yaml.load(config_file) or {}
>>> print(config)
{}

At the top level a YAML file can have 在顶层,YAML文件可以有

  • 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文件:

0

with single scalar floats 0.0 : 单标量浮点数0.0

0.0

with hex scalars 0x0 : 使用十六进制标量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

False

or no ¹: 或者no ¹:

no

or off ¹: off ¹:

off

as well as the empty sequence: 以及空序列:

[

]

to result in config to be an empty dictionary. 导致config为空字典。

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 . ¹这是PyYAML从未更新为1.1到2009年发布的1.2标准的结果。如果是,它也会转换0o0形式的0o0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM