如何修复YAML语法错误:在解析块时未找到预期的“-"指示符? [英] How to fix the YAML syntax error: did not find expected '-' indicator while parsing a block?

查看:1352
本文介绍了如何修复YAML语法错误:在解析块时未找到预期的“-"指示符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在.travis.yml中为Python库编写了一些代码.使用 lint.travis-ci.org ,我知道我的YAML中存在一些缩进问题文件.这是错误所指向的部分

I have some code written in my .travis.yml written for a Python library. Using lint.travis-ci.org, I came to know that there is some indentation problem in my YAML file. Here is the part which the error points to

install:

  - if [[ "${TEST_PY3}" == "false" ]]; then
      pip install Cython;
      python setup.py build; # To build networkx-metis
      mkdir core; # For the installation of networkx core
      cd core;
      git clone https://github.com/orkohunter/networkx.git;
      cd networkx/;
      git checkout addons;
      python setup.py install;
      cd ..;
    fi

我在哪里错了?错误显示

Where am I wrong? The error says

syntax error: (<unknown>): did not find expected '-' indicator while parsing a block collection at line 32 column 3

如果有一个像autopep8这样的工具可以修复YAML文件的缩进,那就太好了.

It would be great if there were a tool like autopep8 to fix the indentation of YAML files.

推荐答案

您的文件中没有32行(可能是因为您从示例中删除了不必要的数据),但是缩进级别指向该行使用fi.

You don't have 32 lines in your file (probably because you stripped non-essential data out of the example), but the indentation level points to the line with fi.

实际上,问题是较早开始的,您要做的是将要采取的操作指定为多行字符串.您可以通过多种方式在YAML中指定它们,但最干净的方法是使用文字标量指示符"|",它保留换行符:

Actually the problem starts earlier and what you want to do is specify the action to take as a multi-line string. You can specify those in YAML in multiple ways but the cleanest is to use the literal scalar indicator "|", which preserves newlines:

install:

  - |
    if [[ "${TEST_PY3}" == "false" ]]; then
      pip install Cython;
      python setup.py build; # To build networkx-metis
      mkdir core; # For the installation of networkx core
      cd core;
      git clone https://github.com/orkohunter/networkx.git;
      cd networkx/;
      git checkout addons;
      python setup.py install;
      cd ..;
    fi

没有针对此类错误的自动YAML重新缩进工具.

There is no automatic YAML re-indentation tool for these kind of errors.

用于Python的reindenters采用有效的代码并使缩进保持一致(替换TAB,每个级别总是相同的缩进).带有语法错误的代码的Python代码重新缩进不起作用,或者可能产生不正确的结果.

Reindenters for Python take working code and make the indentation consistent (replacing TABs, always same indent per level). Python code re-indentation on code with syntax errors, either doesn't work or might produce non-correct results.

YAML的reindenters面临相同的问题:如果输入没有意义(该内容对您和我来说并不总是对程序清楚),该怎么办.仅仅将不能很好地解析为多行标量的所有内容,并不是一个通用的解决方案.

Reindenters for YAML face the same problem: what to do if the input doesn't make sense (and what is clear to you and me, is not always clear to a program). Just making everything that doesn't parse well into a multi-line scalar is not a generic solution.

此外,大多数YAML解析器会丢弃一些有关读取文件的信息,这些信息是您不想因缩进而迷路的,包括EOL注释,手工制作的锚点名称,映射键顺序等.违反规范中的要求.

Apart from that, most YAML parsers throw away some information on reading in the files, that you would not want to get lost by re-indenting, including EOL comments, hand crafted anchor names, mapping key ordering, etc. All without violating the requirements in the specification.

如果要统一缩进(正确的)YAML,则可以使用[ruamel.yaml][2]程序包中的yaml实用程序(免责声明:我是该程序包的作者).您与yaml round-trip .travis.yml一起使用的原始输入将给出:

If you want to uniformly indent your (correct) YAML you can use the yaml utility that is part of the [ruamel.yaml][2] package (disclaimer: I am the author of that package). Your original input used with yaml round-trip .travis.yml would give:

 ...
  in "<byte string>", line 3, column 3:
      - if [[ "${TEST_PY3}" == "false" ... 
      ^
expected <block end>, but found '<scalar>'
  in "<byte string>", line 6, column 7:
          mkdir core; # For the installati ...

不幸的是,发现错误没有太大帮助,通过yaml round-trip .travis.yml运行的正确的.travis.yml版本将告诉您,它在第二次往返时稳定(即,在第一次往返中,多余的空白丢失了).而yaml round-trip .travis.yml --save给您:

Unfortunately not much more helpful in finding the error, the correct .travis.yml version run through yaml round-trip .travis.yml will tell you that it stabilizes on the second round-trip (ie. on the first the extra whitespace is lost). And yaml round-trip .travis.yml --save gives you:

install:
- |
  if [[ "${TEST_PY3}" == "false" ]]; then
    pip install Cython;
    python setup.py build; # To build networkx-metis
    mkdir core; # For the installation of networkx core
    cd core;
    git clone https://github.com/orkohunter/networkx.git;
    cd networkx/;
    git checkout addons;
    python setup.py install;
    cd ..;
  fi

请注意,此# TO build networkx-metis中不是YAML注释.它只是多行字符串的一部分.但是,将保留对第一行之前或最后一行之后的行的注释.

Please note that in this # TO build networkx-metis is not a YAML comment. It is just part of the multi-line string. A comment on a line before the first or after the last would however be preserved.

这篇关于如何修复YAML语法错误:在解析块时未找到预期的“-"指示符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆