【发布时间】:2020-12-24 05:28:50
【问题描述】:
使用 ruamel.yaml 加载 YAML 数据时,YAML 文件中的锚点及其别名在 Python 中是同一个对象:
from ruamel.yaml import YAML
yaml_str = """\
first: &reference [1, 2, 3]
second: *reference
"""
yaml = YAML()
data = yaml.load(yaml_str)
assert(data['first'] is data['second'])
# passes
data['first'].append(4)
print(data['second'])
# output: [1, 2, 3, 4]
我意识到这是一个有意的功能。但是,有没有办法告诉load 在找到别名时改为 copy 别名?我尝试按照this answer 中的说明覆盖yaml.representer.ignore_aliases,但这只是为了写入YAML,而不是从中读取。
【问题讨论】:
标签: python yaml ruamel.yaml