保留YAML中的新行

Cod*_*ict 20 yaml pyyaml

如何格式化这样的YAML文档,以便PyYAML可以正确解析它?

Data: Some data, here and a special character like ':'
      Another line of data on a separate line
Run Code Online (Sandbox Code Playgroud)

我知道':'字符是特殊的所以我必须用引号括起整个东西:

Data: "Some data, here and a special character like ':'
      Another line of data on a separate line"
Run Code Online (Sandbox Code Playgroud)

为了添加新行,我必须添加'\n':

Data: "Some data, here and a special character like ':'\n
      Another line of data on a separate line"
Run Code Online (Sandbox Code Playgroud)

有没有格式化YAML文档,所以我不必添加\n's以便有一个新行?

Nul*_*ion 31

对于多行标量,您可以使用块.字符|表示块的开始.使用:

Data: |
      Some data, here and a special character like ':'
      Another line of data on a separate line
Run Code Online (Sandbox Code Playgroud)


Ant*_*hon 5

如果 NullUserException 的解决方案添加的额外换行符是一个问题,您应该使用:

Data: |-
      Some data, here and a special character like ':'
      Another line of data on a separate line
Run Code Online (Sandbox Code Playgroud)