The literal operator is represented by the pipe (“|”) symbol. It keeps our line breaks but reduces empty lines at the end of the string down to a single line break.
Use quotes in YAML if your value includes special characters. For example, these special characters may require quotes: {, }, [, ], ,, &, :, *, #, ?, |. -, <. >, =, !, %, @, \. It is not necessary to use quotes when a single special character is surrounded by spaces, for example, * with spaces on both sides.
Use >- or |- instead if you don't want a linebreak appended at the end. Use "..." if you need to split lines in the middle of words or want to literally type linebreaks as \n : key: "Antidisestab\ lishmentarianism.
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.
The pipe is used when you want newlines to be kept as newlines.
For more information : https://yaml-multiline.info/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With