【问题标题】:PyYAML throwing unsolvable errorPyYAML 抛出无法解决的错误
【发布时间】:2016-08-01 22:38:03
【问题描述】:

我正在处理 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? 和其他几个线程,但没有任何效果。

【问题讨论】:

    标签: yaml python-3.4 pyyaml


    【解决方案1】:

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

    {      notes: |
    
                    Its not working here
    }
    

    如果您查看YAML specification 并搜索字符串“literal style”,您的第一个匹配项是在 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,但是规范没有改变)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-06
      • 1970-01-01
      • 2019-06-28
      • 2018-01-05
      • 2012-05-15
      • 2013-03-27
      • 1970-01-01
      • 2018-06-05
      相关资源
      最近更新 更多