栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

在pyyaml中,SafeLoader不包含python本机类​​型的加载程序,仅包含yaml规范中定义的类型。你可以看到类型的

SafeLoader
Loader
这里下面的互动样本。

您可以定义一个新的Loader类,该类添加了python元组,但未添加其他类型,因此它仍然非常安全:

import yamlclass 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/unipre': <unbound method Constructor.construct_python_unipre>, 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>}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/425951.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号