开发者

Preserve new lines in YAML

开发者 https://www.devze.com 2023-01-16 23:52 出处:网络
How do I format a YAML document like this so that PyYAML c开发者_如何学JAVAan parse it properly?

How do I format a YAML document like this so that PyYAML c开发者_如何学JAVAan 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?


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


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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号