yaml 中是否有合并键的说明符?

Are there specifiers to merge keys in yaml?

即有没有办法转换这个例子:

myhash:
  - name: name1
    value: value1
myhash:
  - name: name2
    value: value2

进入:

myhash:
  - name: name1
    value: value1
  - name: name2
    value: value2

我一注意到默认情况下 YAML 将其转换为:

myhash:
  - name: name2
    value: value2

在 YAML 1.2 规范中 it is stated "mapping - an unordered association of unique keys to values"(强调我的)。您的密钥不是唯一的,因此发生的情况取决于库的实现(抛出错误,忽略其中一个密钥)。

您的解析器所做的显然是丢弃第一个 key/value 对。你想做的事情不能通过使用 YAML 解析器加载第一个示例来完成。您当然可以编写一个实用程序来拆分不使用 YAML 解析器的文本。

请注意,在 YAML 1.1 中:

It is an error for two equal keys to appear in the same mapping node. In such a case the YAML processor may continue, ignoring the second key: value pair and issuing an appropriate warning.

例如不是 YAML 1.1 解析器 PyYAML 的工作方式:它不会忽略第二个(或后续)键的值,也不会发出警告。