2

这就是我想要做的(代码在 Python 3 中):

import ruamel.yaml as yaml
from print import pprint

yaml_document_with_aliases = """
title: test
choices: &C
  a: one
  b: two
  c: three
---
title: test 2
choices: *C
"""

items = list(yaml.load_all(yaml_document_with_aliases))

结果是:

ComposerError: found undefined alias 'C'

当我使用非基于文档的 YAML 文件时,这可以按预期工作:

import ruamel.yaml as yaml
from print import pprint

yaml_nodes_with_aliases = """
-
  title: test
  choices: &C
    a: one
    b: two
    c: three
-
  title: test 2
  choices: *C
"""

items = yaml.load(yaml_nodes_with_aliases)

pprint(items)

结果:

[{'choices': {'a': 'one', 'b': 'two', 'c': 'three'}, 'title': 'test'},
 {'choices': {'a': 'one', 'b': 'two', 'c': 'three'}, 'title': 'test 2'}]

(无论如何我想完成的事情)


由于现在不可能,我正在使用以下脆弱的解决方法:

def yaml_load_all_with_aliases(yaml_text):
    if not yaml_text.startswith('---'):
        yaml_text = '---\n' + yaml_text
    for pat, repl in [('^', '  '), ('^\s*---\s*$', '-'), ('^\s+\.{3}$\n', '')]:
        yaml_text = re.sub(pat, repl, yaml_text, flags=re.MULTILINE)
    yaml_text = yaml_text.strip()
    return yaml.safe_load(yaml_text)
4

1 回答 1

1

这里的问题是:

title: test
choices: &C
  a: one
  b: two
  c: three
---
title: test 2
choices: *C

不是文档,这是一个文件中的两个 YAML 文档。锚定义&C不会从一个 YAML 文档传递到另一个文档,它只能在文档分隔符之前用完---

如果您愿意在单个 YAML 流中将所有锚点“转移”到以下文档,您可以compose_document在该类上移植一个新方法Composer(即对其进行猴子补丁):

import sys
import ruamel.yaml

yaml_str = """\
title: test
choices: &C
  a: one
  b: two
  c: three
---
title: test 2
choices: *C
"""


def my_compose_document(self):
    self.get_event()
    node = self.compose_node(None, None)
    self.get_event()
    # this prevents cleaning of anchors between documents in **one stream**
    # self.anchors = {}
    return node

ruamel.yaml.composer.Composer.compose_document = my_compose_document

datas = []
for data in ruamel.yaml.safe_load_all(yaml_str):
    datas.append(data)

datas[0]['choices']['a'] = 1
for data in datas:
    ruamel.yaml.round_trip_dump(data, sys.stdout, explicit_start=True)

这使:

---
title: test
choices:
  a: 1
  b: two
  c: three
---
title: test 2
choices:
  a: one
  b: two
  c: three

请注意,这会为您提供带有、和键的 dict副本abc

(如果键顺序和注释的保存很重要,请使用round_trip_load_all代替safe_load_all

于 2016-11-20T08:35:51.493 回答