22

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?

Anthon
  • 69,918
  • 32
  • 186
  • 246
Coding District
  • 11,901
  • 4
  • 26
  • 30

2 Answers2

35

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
NullUserException
  • 83,810
  • 28
  • 209
  • 234
5

If the extra newline that NullUserException's solutions is adding is a problem you should be using:

Data: |-
      Some data, here and a special character like ':'
      Another line of data on a separate line
Anthon
  • 69,918
  • 32
  • 186
  • 246