【发布时间】:2019-11-18 22:58:56
【问题描述】:
我有一个如下所示的 YAML 文件:
!!com.example.hero.YAMLAnimals
animals:
Land: [Cow, Lion]
Sea: [Salmon, Cod]
根据 PyYAML 文档,我应该能够通过子类化 yaml.YAMLOBJECT 来安全地加载 YAMLAnimals 对象。我试过这样做:
class YAMLAnimals(yaml.YAMLObject):
yaml_tag = u'!com.example.hero.YAMLAnimals'
def __init__(self, animals):
self.animals = animals
但是,当我尝试使用 yaml.load() 或 yaml.safe_load() 解析文件时,我收到以下错误:
could not determine a constructor for the tag 'tag:yaml.org,2002:com.example.hero.YAMLAnimals'
谢谢!
【问题讨论】:
-
在文档中看到了这个:
# In Python 3, do not use the 'u' prefix另外,不是 YAML 专家,但在你的课堂上注意到你的字符串中只有一个 bang!。 -
可能是一个未解决的问题:github.com/yaml/pyyaml/issues/266