PyYAML 抛出无法解决的错误

PyYAML throwing unsolvable error

我正在处理 YAML 文件,但我一直在使用“|”对于文字引号。

我正在使用 PyYAML。

这里的主要问题是它适用于第一级 "Dictionary" 输入以下代码,但对于第二级 "notes" 密钥它不起作用。

我试过使用“>”“|+”“|-”但是没有用。

Description: |

    This is a sample text showing that it works fine here.

Signatures:
    -   {   
            returnValue:        'placeholder',
            notes: |

                Its not working here
    }
    -   {   
            returnValue:        'another placeholder',
            notes: ' 
                     This is working here 

                    '
        }

我检查了 http://yaml-online-parser.appspot.com/ , https://nodeca.github.io/js-yaml/ 和其他人的语法,我得到的错误是

错误: 在扫描下一个令牌时 找到字符“|”不能启动任何令牌 在“”中,第 8 行,第 24 列: 注:|

我浏览了线程 In YAML, how do I break a string over multiple lines? 和其他几个,但没有任何效果。

首先,始终制作引发错误的最小示例:

{      notes: |

                Its not working here
}

如果您查看 YAML specification 并搜索字符串 "literal style",您的第一个命中是在 Cotents 的 Table 中,第 8.1.2 节是描述的一部分块样式

您的代码使用 { } 指定映射的流样式,其中您不能使用块样式文字标量。

你应该使整个 YAML 一致的块样式(删除映射元素之间的 {},):

Description: |

    This is a sample text showing that it works fine here.

Signatures:
    -   returnValue: placeholder
        notes: |

            Its not working here
    -   returnValue:  another placeholder
        notes: '
                This is working here

                '

顺便说一句,因为文字标量的默认 chomping 是 clipping,如果您在此类标量的末尾添加额外的空行,它不会改变任何东西。

(PyYAML 仅支持 YAML 1.1,但关于此规范并未更改)。