【问题标题】:Preserve new lines in YAML在 YAML 中保留新行
【发布时间】:2011-04-14 11:56:31
【问题描述】:

如何格式化这样的 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”来换行?

【问题讨论】:

    标签: yaml pyyaml


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 2020-11-21
        • 2018-05-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多