python - 在 Python 中解析具有重复 anchor 的 YAML

标签 python yaml

我刚刚开始使用 YAML 和 Python,我正在尝试用 Python 解析包含 anchor 和别名的 YAML。
在此 YAML 中,我覆盖 anchor 以使某些节点具有不同的值。

我的 YAML 示例:

Some Colors: &some_colors
 color_primary: &color_primary "#112233FF"
 color_secondary: &color_secondary "#445566FF"

Element: &element
 color: *color_primary

Overwrite some colors: &overwrite_colors
 color_primary: &color_primary "#000000FF"

Another element: &another_element
 color: *color_primary

预期结果为(JSON 格式):

{
    "Some Colors": {
        "color_primary": "#112233FF",
        "color_secondary": "#445566FF"
    },
    "Element": {
        "color": "#112233FF"
    },
    "Overwrite some colors": {
        "color_primary": "#000000FF"
    },
    "Another element": {
        "color": "#000000FF"
    }
}

I tested the above YAML snippet here

根据我在 YAML 文档中阅读的内容;这应该从 1.1 版开始就可以实现(我认为),但至少 YAML 1.2 版应该支持它。

但每当我尝试使用 PyYAML(使用 yaml.load() )或 ruamel,yaml 解析 YAML 时包(带有 ruamel.yaml.load() ),我收到“重复 anchor ”错误。

我在这里做错了什么?以及如何解决这个问题?

编辑:

ruamel的帮助下的所有者我已经找到了上述问题的解决方案。

截至ruamel v0.12.3 以上工作正常,但您会收到 ReusedAnchorWarning s.
可以使用以下代码片段来抑制这些警告:

import warnings
from ruamel.yaml.error import ReusedAnchorWarning

warnings.simplefilter("ignore", ReusedAnchorWarning)

在应得的地方给予学分;所有这些都转到ruamel的所有者。


作为附加问题;当我将上述 YAML 修改为 (注意 // <-- Added this 处的更改):

Some Colors: &some_colors
 color_primary: &color_primary "#112233FF"
 color_secondary: &color_secondary "#445566FF"

Element: &element
 color: *color_primary

Overwrite some colors: &overwrite_colors
 <<: *some_colors   // <-- Added this to include 'color_secondary' as well
 color_primary: &color_primary "#000000FF"

Another element: &another_element
 color: *color_primary

输出是:

{
    "Some Colors": {
        "color_primary": "#000000FF",
        "color_secondary": "#445566FF"
    },
    "Element": {
        "color": "#112233FF"
    },
    "Overwrite some colors": {
        "color_primary": "#000000FF",
        "color_secondary": "#445566FF"
    },
    "Another element": {
        "color": "#445566FF" // <-- Now the value is 'color_secondary' instead of 'color_primary'?
    }
}

为什么是colorAnother element查看 color_secondary 的值反而?

还有什么方法可以解决这个问题吗?

最佳答案

首先,你没有做错任何事。 PyYAML 在这里做错了。这很可能是因为使用相同名称转储 anchor 对于 PyYAML 转储程序来说是一种错误情况。如果您有一个自引用的 Python 结构:

 a = dict(x=1)
 a['y'] = a

然后 PyYAML(和 ruamel.yaml)将为您创建一个唯一的 anchor 名称。如果此名称不是唯一的,则取决于该名称用作别名的位置。因此,怀疑任何重用的 anchor 名称是有意义的,因为这可能指向 YAML 序列化代码中的错误,但这并不违反规范(根据 YAML 1.0 规范(第 3.2.2.2 节),重用已经可以了) ).

A bug report对于 python-yaml Debian 模块,自 2009 年以来就存在,但我还没有发现它是否在上游结束。

正如您所说,这已在 ruamel.yaml 0.12.3 中解决


要回答你的第二个问题,那只是因为“Best Online YAML Converter”不是,并且解析错误。如果合并行上有 YAML 注释,它甚至会抛出错误:

 <<: *some_colors   # <-- Added this to include 'color_secondary' as well

这在 ruamel.yaml (0.12.3) 中按预期解析:

import sys
import ruamel.yaml
import warnings
from ruamel.yaml.error import ReusedAnchorWarning
warnings.simplefilter("ignore", ReusedAnchorWarning)

yaml_str = """\
Some Colors: &some_colors
 color_primary: &color_primary "#112233FF"
 color_secondary: &color_secondary "#445566FF"

Element: &element
 color: *color_primary

Overwrite some colors: &overwrite_colors
 <<: *some_colors   # <-- Added this to include 'color_secondary' as well
 color_primary: &color_primary "#000000FF"

Another element: &another_element
 color: *color_primary
"""


data = ruamel.yaml.safe_load(yaml_str)
ruamel.yaml.round_trip_dump(data, sys.stdout)

给出:

Some Colors:
  color_primary: '#112233FF'
  color_secondary: '#445566FF'
Overwrite some colors:
  color_primary: '#000000FF'
  color_secondary: '#445566FF'
Another element:
  color: '#000000FF'    # <- not #445566FF
Element:
  color: '#112233FF'

(手动添加的评论)

如果你想使用新的 API 和安全加载器,请确保指定 pure=True,否则 ruamel.yaml 的 libyaml 副本(仍然有这个错误)将被使用,你会得到 ComposerError:

yaml = ruamel.yaml.YAML(typ='safe')
data = yaml.load(yaml_str)
yaml.dump(data, sys.stdout)

关于python - 在 Python 中解析具有重复 anchor 的 YAML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39013993/

相关文章:

mysql - 在 Yii 中,有没有办法用类似 YAML 或 XML 的方式编写模式?

go - 使用 Go 读写 Yaml 文件

java - 从 Java 中的 yaml 读取 map 得到 null

python - 将对象从解析的 csv 转换为 int Python

Python-docx 无法使用现有文档 - 没有名称为 'Title' 的样式

Python,使用两个路径和参数创建快捷方式

ruby-on-rails - 如何在 YAML 文件中使用变量?

node.js - 在 nodejs 的 swagger.yaml 中使用环境变量

python - numpy python 中的乘法()

python - 使用pandas包在python中合并多个excel文件的数据