简体   繁体   中英

Multi-line in a sequence in 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.

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'

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM