Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multi-line in a sequence in YAML

Tags:

syntax

yaml

I would like to have multiple lines in a sequence in YAML. This is how I do it, but I have issues with parsing it in python:

Element: |
   - multiple lines
     come here

Doing it this way, when I parse it with Python, I still see the - in the parsed data. It seems that YAML does not understand this is a list.

like image 843
Afshin Avatar asked Dec 21 '18 23:12

Afshin


People also ask

How do you write multiple lines in YAML?

Multi-line blocks and long strings. YAML also supports writing strings on multiple lines in your YAML, with two different styles: literal and folded. Those are represented by a | (for literal style) or > (for folded style) header line (see examples below), and we will go into their differences soon.

How do you insert a line break in YAML?

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. \n\nGet on it."

Can you have new lines in YAML?

If you would like them to be kept as newlines, use the literal style, indicated by a pipe ( | ). If instead you want them to be replaced by spaces, use the folded style, indicated by a right angle bracket ( > ). (To get a newline using the folded style, leave a blank line by putting two newlines in.


2 Answers

Your input is not a list, YAML only knows about mappings (constructed as a Python dict and sequences (constructed as a Python list).

Normally - is the block sequence entry indicator, But since you start a block style literal on the first line as the value for the key Element, because of the |, everything following it that is indented is part of this scalar (constructed as a Python string).

What you want to do is bring the indicator outside of the literal scalar:

Element: 
- |
  multiple lines
  come here

If you load that in Python in a variable data then data['Element'][0] will be the string 'multiple lines\ncome here\n'. That is: every newline in your literal scalar will be a newline in your string, and there will be a single final newline on that string independent of how many empty lines follow (this is clipping). If you want the end to have no newline, then use |- (stripping), and if you want all newlines until outdenting then use |+ (keeping). Those additions to the | are called chomping indicators.

If you have the above in a file called input.yaml:

import sys
from pathlib import Path
import ruamel.yaml

input = Path('input.yaml')

yaml = ruamel.yaml.YAML(typ='safe')
data = yaml.load(input)
print(f'{data["Element"][0]!r}')  # print the representation, so you can see where the newlines are

which gives:

'multiple lines\ncome here\n'
like image 88
Anthon Avatar answered Nov 14 '22 08:11

Anthon


Use this syntax (for the yaml Python package, at least)

stuff:
  - 'this is a multiline
  string'

In other words quote the string and unindent its continuation.

like image 43
robinsax Avatar answered Nov 14 '22 07:11

robinsax