Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preserve new lines in YAML

Tags:

How do I format a YAML document like this so that PyYAML can parse it properly?

Data: Some data, here and a special character like ':'       Another line of data on a separate line 

I know that the ':' character is special so I have to surround the whole thing in quotations like so:

Data: "Some data, here and a special character like ':'       Another line of data on a separate line" 

And in order to add a new line, I have to add '\n':

Data: "Some data, here and a special character like ':'\n       Another line of data on a separate line" 

Is there anyway to format the YAML document so I don't have to add the '\n's in order to have a new line?

like image 863
Coding District Avatar asked Sep 20 '10 03:09

Coding District


People also ask

How do I add a new line 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.

How do you write a multiline in YAML?

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.

Can YAML have blank lines?

In general the order of keys in a YAML file does not matter. Neither do blank lines. Indentation may be with any number of spaces, as long as it is consistent throughout the file. Web PaaS examples by convention use four-space indentation.

What is string in YAML?

In YAML, strings can be represented in the following several ways: Single-quoted scalars. Double-quoted scalars. Unquoted scalars. Folded scalars.


1 Answers

For multi-line scalars, you can use blocks. The character | denotes the start of a block. Use:

Data: |       Some data, here and a special character like ':'       Another line of data on a separate line 
like image 139
NullUserException Avatar answered Oct 01 '22 18:10

NullUserException