What is the use of the pipe symbol in YAML?

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 2+ instead of 6+ because the last block-level element is the mapping final: and the literal is indented 2 from it. Updated with correction from @bramvi.

Leave a Comment