gpt4 book ai didi

python - 如何使用 pyYAML 将 python 元组添加到 YAML 文件?

转载 作者:太空狗 更新时间:2023-10-29 21:32:25 26 4
gpt4 key购买 nike

标题是不言自明的。

当我将元组保存到 YAML 文件时,我得到如下所示的内容:

ambient:  !!python/tuple [0.3, 0.3 ,0.3]

当我尝试使用 yaml.safe_load(file_object) 加载它时,我不断收到错误消息:

yaml.constructor.ConstructorError:  could not determine a constructor for the tag 'tag:yaml.org,2002:python/tuple'

需要做什么?

最佳答案

在 pyyaml 中,SafeLoader 不包含 python 原生类型的加载器,仅包含 yaml 规范中定义的类型。您可以在下面的交互示例中看到 SafeLoaderLoader 的类型。

你可以定义一个新的 Loader 类,在 python 元组中添加,而不是其他类型,所以它应该仍然很安全:

import yaml

class PrettySafeLoader(yaml.SafeLoader):
def construct_python_tuple(self, node):
return tuple(self.construct_sequence(node))

PrettySafeLoader.add_constructor(
u'tag:yaml.org,2002:python/tuple',
PrettySafeLoader.construct_python_tuple)

doc = yaml.dump(tuple("foo bar baaz".split()))
print repr(doc)
thing = yaml.load(doc, Loader=PrettySafeLoader)
print thing

导致:

'!!python/tuple [foo, bar, baaz]\n'
('foo', 'bar', 'baaz')

有关与 SafeLoader 和 Loader 类关联的构造函数,请参见下文。

>>> yaml.SafeLoader.yaml_constructors
{None: <unbound method SafeConstructor.construct_undefined>,
u'tag:yaml.org,2002:binary': <unbound method SafeConstructor.construct_yaml_binary>,
u'tag:yaml.org,2002:bool': <unbound method SafeConstructor.construct_yaml_bool>,
u'tag:yaml.org,2002:float': <unbound method SafeConstructor.construct_yaml_float>,
u'tag:yaml.org,2002:int': <unbound method SafeConstructor.construct_yaml_int>,
u'tag:yaml.org,2002:map': <unbound method SafeConstructor.construct_yaml_map>,
u'tag:yaml.org,2002:null': <unbound method SafeConstructor.construct_yaml_null>,
u'tag:yaml.org,2002:omap': <unbound method SafeConstructor.construct_yaml_omap>,
u'tag:yaml.org,2002:pairs': <unbound method SafeConstructor.construct_yaml_pairs>,
u'tag:yaml.org,2002:seq': <unbound method SafeConstructor.construct_yaml_seq>,
u'tag:yaml.org,2002:set': <unbound method SafeConstructor.construct_yaml_set>,
u'tag:yaml.org,2002:str': <unbound method SafeConstructor.construct_yaml_str>,
u'tag:yaml.org,2002:timestamp': <unbound method SafeConstructor.construct_yaml_timestamp>}

>>> yaml.Loader.yaml_constructors
{None: <unbound method SafeConstructor.construct_undefined>,
u'tag:yaml.org,2002:binary': <unbound method SafeConstructor.construct_yaml_binary>,
u'tag:yaml.org,2002:bool': <unbound method SafeConstructor.construct_yaml_bool>,
u'tag:yaml.org,2002:float': <unbound method SafeConstructor.construct_yaml_float>,
u'tag:yaml.org,2002:int': <unbound method SafeConstructor.construct_yaml_int>,
u'tag:yaml.org,2002:map': <unbound method SafeConstructor.construct_yaml_map>,
u'tag:yaml.org,2002:null': <unbound method SafeConstructor.construct_yaml_null>,
u'tag:yaml.org,2002:omap': <unbound method SafeConstructor.construct_yaml_omap>,
u'tag:yaml.org,2002:pairs': <unbound method SafeConstructor.construct_yaml_pairs>,
u'tag:yaml.org,2002:python/bool': <unbound method Constructor.construct_yaml_bool>,
u'tag:yaml.org,2002:python/complex': <unbound method Constructor.construct_python_complex>,
u'tag:yaml.org,2002:python/dict': <unbound method Constructor.construct_yaml_map>,
u'tag:yaml.org,2002:python/float': <unbound method Constructor.construct_yaml_float>,
u'tag:yaml.org,2002:python/int': <unbound method Constructor.construct_yaml_int>,
u'tag:yaml.org,2002:python/list': <unbound method Constructor.construct_yaml_seq>,
u'tag:yaml.org,2002:python/long': <unbound method Constructor.construct_python_long>,
u'tag:yaml.org,2002:python/none': <unbound method Constructor.construct_yaml_null>,
u'tag:yaml.org,2002:python/str': <unbound method Constructor.construct_python_str>,
u'tag:yaml.org,2002:python/tuple': <unbound method Constructor.construct_python_tuple>,
u'tag:yaml.org,2002:python/unicode': <unbound method Constructor.construct_python_unicode>,
u'tag:yaml.org,2002:seq': <unbound method SafeConstructor.construct_yaml_seq>,
u'tag:yaml.org,2002:set': <unbound method SafeConstructor.construct_yaml_set>,
u'tag:yaml.org,2002:str': <unbound method SafeConstructor.construct_yaml_str>,
u'tag:yaml.org,2002:timestamp': <unbound method SafeConstructor.construct_yaml_timestamp>}

关于python - 如何使用 pyYAML 将 python 元组添加到 YAML 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9169025/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com