What is the use of the pipe symbol in YAML?

后端 未结 2 478
温柔的废话
温柔的废话 2021-01-30 07:31

I am new to yaml, and I have a question about the pipe symbol (|) used for multiple lines. Does YAML have any syntax like the one below?

test:

相关标签:
2条回答
  • 2021-01-30 08:14

    The pipe is used when you want newslines to be kept as newlines.

    For more information : https://yaml-multiline.info/

    0 讨论(0)
  • 2021-01-30 08:22

    The pipe symbol at the end of a line in YAML signifies that any indented text that follows should be interpreted as a multi-line scalar value. See the YAML spec.

    Specifically, the pipe indicates that (except for the indentation) the scalar value should be interpreted literally in such a way that preserves newlines. Conversely, the > character indicates that multi-line "folded" scalar follows, meaning that newlines are converted to spaces. For example:

    >>> import yaml
    >>> yaml.load("""
    ... |
    ...  This is a multi-line
    ...  literal style scalar.
    ... """)
    'This is a multi-line\nliteral style scalar.\n'
    >>> yaml.load("""
    ... >
    ...  This is a multi-line
    ...  folded scalar; new lines are folded into
    ...  spaces.
    ... """)
    'This is a multi-line folded scalar; new lines are folded into spaces.\n'
    

    The 6+ part is the indentation indicator (an explicit specification of how many spaces of indentation should be used) with the "chomping indicator" + which controls how extra whitespace at the end of the scalar literal should be handled.

    The error you're getting is a tricky one: It's because indentation should be relative to the current block-level element. So in this case it should be 4+ instead of 6+ because the last block-level element is the array item (specified by -) and the literal is indented 4 from it. Somewhat surprisingly the final: | mapping is not considered a block element even though its value is multi-lined. It sort of makes sense if you think about it -- it's still just a 'one-liner' "key: value" mapping. The value just happens to be using a special syntax for multi-line scalar values. Confusing, but somehow consistent...

    0 讨论(0)
提交回复
热议问题