- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
标题是不言自明的。
当我将元组保存到 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 规范中定义的类型。您可以在下面的交互示例中看到 SafeLoader
和 Loader
的类型。
你可以定义一个新的 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/
我想在我的 pip 项目中使用 PyYaml,但在将其用作依赖项时遇到了麻烦。主要问题是 pip 中的 PyYaml 不是跨平台安装。 如何使用 pip 安装 pyyaml 以使其正常工作。请注意,在
这个问题在这里已经有了答案: Pip install from pypi works, but from testpypi fails (cannot find requirements) (2 个回
我想用 PyYaml 解释以下 YAML 字符串: - !Table header: - !Column - !Paragrap
yaml.load将数字加载为 Python 浮点数。我找不到一种直接的方法来覆盖它。 比较 json.load , 允许 parse_float=Decimal如果你想将浮点数解析为 decimal
我认为加载 .yml file 时出现内存泄漏使用库 PyYAML。 我已按照以下步骤操作: import yaml d = yaml.load(open(filename, 'r')) 进程使用的内
我需要使用 pyyaml 将 dict 导出到 yaml,不带“-” export_dash_dict = {} export_dash_dict["dashboards"] = []
在下面的代码中,我创建了 net_plan_dict 变量字典并将其转换为 YAML 格式文件。在字典中,我有一个名为 addresses 的字段,它是一个包含三个元素的数组。创建 YAML 文件后,
我构建 libyaml 并将其安装到本地区域: yaml-0.1.5 $ ./configure --prefix=/usr/local/sqlminus yaml-0.1.5 $ make inst
这个问题已经有答案了: New PyYAML version breaks on most custom python objects - RepresenterError (1 个回答) 已关闭 5
我正在尝试理解关于 https://pyyaml.org/wiki/PyYAML 的声明那: PyYAML features - a complete YAML 1.1 parser. In part
看起来好像 PyYAML 将字符串 10:01 解释为以秒为单位的持续时间: import yaml >>> yaml.load("time: 10:01") {'time': 601} 官方文档没有
尝试获取 YAML 文档以解析为我可以操作的 python 字典对象。我安装了 pip install pyyaml。 import yaml yamlstring = "some: var \n a
所以我有一个 yaml 文件,里面有很多琐碎的问题和一个答案列表。但是,每当我尝试加载此文件并使用 pyyaml 将内容转储到 python 中时,它都会向后转储它们。我不确定这是我的 yaml 文件
当用很长的键转储字典时,我得到这样的结果: ? ns=4;s=|var|CODESYS Control for Raspberry Pi SL.Application.GVL.Data.DateTim
我想将一些配置参数从 YAML 直接映射到 Python 参数名称。只是想知道是否有一种方法无需编写额外代码(之后修改键)让 YAML 解析器用下划线“_”替换键中的破折号“-”。 some-para
这个问题在这里已经有了答案: Parsing YAML, return with line number (4 个答案) 关闭 1 年前。 有什么方法可以配置 PyYAML 以便我可以获得与给定节点
我有以下 Python 2.6 程序和 YAML 定义(使用 PyYAML ): import yaml x = yaml.load( """ product:
我有以下 YAML 文件: --- my_vars: my_env: "dev" my_count: 3 当我用 PyYAML 读取它并再次转储它时,我得到以下输出: --- my_vars:
我正在开发一个使用 Unity3D 游戏引擎的项目。对于某些管道要求,最好能够使用 Python 从外部工具更新某些文件。 Unity 的 meta 和 anim 文件在 YAML 中,所以我认为使用
我有一个 python 项目,我想在其中使用 YAML (pyYaml 3.11),特别是因为它“漂亮”并且用户可以在必要时轻松地在文本编辑器中进行编辑。不过,我的问题是,如果我将 YAML 引入 p
我是一名优秀的程序员,十分优秀!