2012-02-06 57 views
7

标题不言自明。如何使用pyYAML将python元组添加到YAML文件中?

当我一个元组保存到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' 

需要做什么?

回答

9

在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>} 
+0

这很有道理。非常感谢你! – blz 2012-02-07 12:07:32

0

至少根据the PyYAML documentation

功能yaml.safe_load限制这种能力像整数或列表简单Python对象。

列表as you can see in the source比较广泛,但不包括tag:yaml.org,2002:python/tuple

看来,如果您在YAML文件中生成!!python/tuple类型,那么您使用的是dump()而不是safe_dump()。如果是这种情况,您应该切换到使用load()代替safe_load(),因为由dump()创建的文件不能保证可由safe_load()加载。 (见description of safe_dump())。

+1

我添加缺少的词*不*,但我不知道是否你实际上并没有想要制定它倒过来... – 2012-02-07 00:44:25

+0

@NiklasB:谢谢。 – ig0774 2012-02-07 00:45:12

+0

在合理的人认为使用yaml.loader(与safe_loader相反)的危险条件下,在什么情况下?我正在编写一个加载YAML文件中定义的资产的游戏。恶意下载可能会覆盖我的YAML文件,从而导致我的游戏加载危险代码,但这似乎是最终用户的问题。我没有太多的工作可以......对吧? – blz 2012-02-07 12:11:18

相关问题