保留YAML中的新行

时间:2010-09-20 03:42:31

标签: yaml pyyaml

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

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

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

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

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

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

是否有格式化YAML文档,所以我不必添加“\n”以获得新行?

2 个答案:

答案 0 :(得分:31)

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

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

答案 1 :(得分:2)

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

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