问题描述
我正在尝试结合使用锚和别名,以便将值重复使用不同名称的多个容器中.
I am trying to combine anchors and aliases in order to reuse values into several containers, under different names.
我尝试了以下代码:
FWL_GV_NANSEN: &fwl_gv_nansen
dtype: float
value: 2715.0
FWL_GV_E3_2: &fwl_gv_e32 *fwl_gv_nansen
我们的目标仅仅是让另一个变量FWL_GV_E3_2包含与FWL_GV_NANSEN相同的信息,稍后我将对此进行引用.
the goal is simply to have another variable FWL_GV_E3_2 containing the same information than FWL_GV_NANSEN that I could refer to later on.
与在Python(或其他版本)中定义的相同:
Just the same than defining in Python (or other):
a = 5.0
b = a
c = b
但这会触发以下错误消息:
But this triggers the following error message:
没有办法将别名的内容分配给用于定义新锚点的变量(通过不同的变量传播初始值吗?
Is there no way to assign the content of aliases to variable used to defined a new anchor (propagating the initial values through different variables?
PS:也许YAML并不是最好的语言,因为例如使用python的变量将是微不足道的,但是我必须使用YAML
PS: maybe YAML is not the best language for this since it would be trivial using python's variables for example but I have to use YAML
推荐答案
YAML节点属性(即标记和锚点)只能出现在真实"节点上:集合(块或流)或标量.
The YAML node properties (i.e. tags and anchors) can only occur on "real" nodes: collections (block or flow) or scalars.
从生产规则中可以看到, c-ns-properties
仅可在此处使用,但别名部分也明确声明:
You can see from the production rules that c-ns-properties
can be used only there, but section for aliases also states explicitly:
如果您的YAML解析器允许您访问原始锚点/别名或某些锚点到节点/对象的映射(例如我的 ruamel.yaml
,>用于往返的Python软件包)是使用带标记的标量:
What is possible if your YAML parser gives you access to the original anchor/aliases, or some anchor-to-node/object mapping (such as my ruamel.yaml
package for Python when used for round-tripping), is to use a tagged scalar:
FWL_GV_NANSEN: &fwl_gv_nansen
dtype: float
value: 2715.0
FWL_GV_E3_2: &fwl_gv_e32 !ref fwl_gv_nansen
使用!ref
对象的构造函数解析标量 fwl_gv_nansen
的标量,并带有一些 application 特定代码.通常,锚和别名事件是在作曲之前由作曲程序加载程序步骤 解决的(如果这样,则不再可用信息).
with the constructor of the !ref
object resolving the scalar fwl_gv_nansen
, with some application specific code. Normally the anchor and alias events are resolved by the composer loader step before composing (and if so that information is no longer available).
这篇关于在一行中将锚和别名组合在一起会触发parserError:'Expected< block end> ;,但找到了'< alias>'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!