PyYAML抛出无法解决的错误

时间:2016-04-11 16:27:09

标签: yaml python-3.4 pyyaml

我正在使用YAML文件而且我一直在使用" |"对于字面引号。

我正在使用PyYAML。

这里的主要问题是它适用于第一级"字典"键入以下代码,但对于第二级"注意"关键它不起作用。

我尝试过使用">" " | +" " | - "但没有任何效果。

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?和其他一些人,但没有任何效果。

1 个答案:

答案 0 :(得分:2)

首先,始终制作抛出错误的最小示例:

{      notes: |

                Its not working here
}

如果你查看YAML specification并搜索字符串"字面样式"你的第一个打击是在Cotents表中,第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

                '

顺便说一句,因为默认选择文字标量是clipping,如果你在这些标量的末尾添加额外的空行,它就不会改变任何东西。

(PyYAML仅支持YAML 1.1,但是这个规范没有改变)。