【问题标题】:Is it possible to anchor a Literal Block in YAML?是否可以在 YAML 中锚定文字块?
【发布时间】:2014-08-30 15:54:38
【问题描述】:

我正在做的是创建几个像下面这样的文字块,并将它们放在一个列表中。

literal1: |
    line
    of 
    text and stuff

literal2: |
    ...

现在我想不通的部分是将它们放在一个列表中。我想我将使用锚点和别名,但它们似乎不适用于文字块。

这样做是不行的

literal1: | &literal1
    line
    of
    text and stuff

它吐出一个错误。而且我宁愿不必创建一个字典

literals: &literal1
    literal1: |
        ....

为此工作。我确信有一种简单的方法可以做到这一点,但我似乎找不到它。

【问题讨论】:

    标签: python yaml pyyaml


    【解决方案1】:

    这是你想做的吗?

    literal1: &literal1 |
        line
        of 
        text and stuff
    
    literal2: &literal2 |
        another line
        of text and new stuff
    
    literals:
    -  *literal1
    -  *literal2    
    

    以下程序将打印...

    ['line\nof \ntext and stuff\n', 'another line\nof text and new stuff\n']

    import yaml
    
    data="""
    literal1: &literal1 |
        line
        of 
        text and stuff
    
    literal2: &literal2 |
        another line
        of text and new stuff
    
    literals:
    -  *literal1
    -  *literal2    
    """
    
    pydata = yaml.load(data)
    literals = pydata [ 'literals' ]
    
    print ( type(literals), literals )
    

    【讨论】:

    • 我似乎找不到证据证明这是有效的 YAML 语法。我想使用 yaml 为 GitLab CI 作业做一些非常相似的事情,但这没有通过验证器。
    猜你喜欢
    • 1970-01-01
    • 2019-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-05
    • 2015-02-13
    • 1970-01-01
    • 2016-04-09
    相关资源
    最近更新 更多