yaml 重复节点是一个关键 [英] yaml repeated node that is a key

查看:36
本文介绍了yaml 重复节点是一个关键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 yaml 中有一个类别层次结构.没有值,只有键.如何制作重复节点?例如,我需要 Entertainment 类别和 Tech 类别中的 Apps.通常重复的节点被标记为值,如下例所示:

I have a hierarchy of categories in yaml. There are no values, only keys. How can I make a repeated node? For example, I want Apps in both the Entertainment category and the Tech category. Usually repeated nodes are noted as values, as in the following example:

hr:
  - Mark McGwire
  # Following node labeled SS
  - &SS Sammy Sosa
rbi:
  - *SS # Subsequent occurrence
  - Ken Griffey

但就我而言,我只有键,没有值.我尝试了以下操作,但是当我使用 pyyaml 将它加载到 python 中时,我得到了一个包含 App: None 的字典,用于引用 App.

But in my case I have only keys, not values. I tried the following, but when I loaded it into python using pyyaml I got a dictionary containing App: None for the reference to App.

Entertainment:
    Music:
        Country:
        Hip hop:
        Pop:
        Rock:
        Alternative:
    &Apps Apps:
        Games:
        Productivity:
        Travel:
        Finance:
        Lifestyle:
Goods:
    Tech:
        Visual:
            Projectors:
            TVs:
        Phones:
            Smart phones:
            Feature phones:
        *Apps:

推荐答案

AFAIK 锚点可以应用于节点(即数据、序列和映射),但不一定应用于节点内的键.所以Apps键不能被锚定,但Apps包含的映射是公平的.这对我有用:

AFAIK anchors can be applied to nodes (i.e. data, sequences, and mappings), but not necessarily to a key within a node. So the Apps key cannot be anchored, but the mapping that Apps contains is fair game. This works for me:

Entertainment:
    Music:
        Country:
        Western:
    Apps: &AppsKeys
        Games:
        Travel:
Goods:
    Tech:
        Visual:
        Phones:
        Apps: *AppsKeys

我想使用 Apps 关键字有点多余.可能还有一些方法可以将锚点应用于我不知道的单个密钥.但我的猜测是这是不可能的,因为它不代表一个基本单位(又名节点).

I guess using the Apps keyword is slightly redundant. There may also be some way to apply an anchor to an individual key that I don't know about. But my guess is that it's not possible, since it doesn't represent a fundamental unit (aka node).

这篇关于yaml 重复节点是一个关键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆