PyYAML错误:" yaml.scanner.ScannerError:这里不允许映射值"

时间:2015-09-17 23:24:29

标签: python yaml python-3.4 pyyaml

我在解析YAML时遇到了一个奇怪的错误:

yaml.scanner.ScannerError: mapping values are not allowed here

根据YAML Lint

,我尝试阅读的YAML文件有效

另一个奇怪的事情是它在我的笔记本电脑(Arch Linux)上工作正常但在服务器(Ubuntu)上却没有。 PyYAML版本在两台机器上都是一样的。

我见过the other posts on stackoverflow where people were missing the space after the colon,但我没有遗漏任何空格。

这是完整的YAML文件:

pipeline:
    - read:
            input: /home/omnibrain/projects/company/data/data.csv
            output: some_data
    - filter:
            input: some_data
            filtername: latlng_filter
            minlat: 32.5
            maxlat: 32.9
            minlng: -117.4
            maxlng: -117.0
    - enhance:
            input: some_data
            enhancername: geo_enhancer
            fields: zip
    - write:
            input: some_data
            writername: csv_writer
            output_dir: /home/omnibrain/outputs
            columns: [id, latitude, longitude, zip, networktype]
            filename: example1 # the output filename

这是完整的堆栈跟踪:

Traceback (most recent call last):
  File "/usr/local/bin/someproject", line 9, in <module>
    load_entry_point('someproject==0.0.1', 'console_scripts', 'someproject')()
  File "/usr/local/lib/python3.4/dist-packages/someproject-0.0.1-py3.4.egg/someproject/__init__.py", line 19, in main
    pipeline.Pipeline(parser.parse_args().scriptfile).start()
  File "/usr/local/lib/python3.4/dist-packages/someproject-0.0.1-py3.4.egg/someproject/pipeline/pipeline.py", line 20, in __init__
    self._raw_pipeline = self._parse_yaml(yamlscript)
  File "/usr/local/lib/python3.4/dist-packages/someproject-0.0.1-py3.4.egg/someproject/pipeline/pipeline.py", line 55, in _parse_yaml
    data = yaml.load(yamlscript)
  File "/usr/local/lib/python3.4/dist-packages/PyYAML-3.11-py3.4-linux-x86_64.egg/yaml/__init__.py", line 72, in load
  File "/usr/local/lib/python3.4/dist-packages/PyYAML-3.11-py3.4-linux-x86_64.egg/yaml/constructor.py", line 35, in get_single_data
  File "/usr/local/lib/python3.4/dist-packages/PyYAML-3.11-py3.4-linux-x86_64.egg/yaml/composer.py", line 36, in get_single_node
  File "/usr/local/lib/python3.4/dist-packages/PyYAML-3.11-py3.4-linux-x86_64.egg/yaml/composer.py", line 55, in compose_document
  File "/usr/local/lib/python3.4/dist-packages/PyYAML-3.11-py3.4-linux-x86_64.egg/yaml/composer.py", line 84, in compose_node
  File "/usr/local/lib/python3.4/dist-packages/PyYAML-3.11-py3.4-linux-x86_64.egg/yaml/composer.py", line 133, in compose_mapping_node
  File "/usr/local/lib/python3.4/dist-packages/PyYAML-3.11-py3.4-linux-x86_64.egg/yaml/composer.py", line 82, in compose_node
  File "/usr/local/lib/python3.4/dist-packages/PyYAML-3.11-py3.4-linux-x86_64.egg/yaml/composer.py", line 111, in compose_sequence_node
  File "/usr/local/lib/python3.4/dist-packages/PyYAML-3.11-py3.4-linux-x86_64.egg/yaml/composer.py", line 84, in compose_node
  File "/usr/local/lib/python3.4/dist-packages/PyYAML-3.11-py3.4-linux-x86_64.egg/yaml/composer.py", line 133, in compose_mapping_node
  File "/usr/local/lib/python3.4/dist-packages/PyYAML-3.11-py3.4-linux-x86_64.egg/yaml/composer.py", line 84, in compose_node
  File "/usr/local/lib/python3.4/dist-packages/PyYAML-3.11-py3.4-linux-x86_64.egg/yaml/composer.py", line 127, in compose_mapping_node
  File "/usr/local/lib/python3.4/dist-packages/PyYAML-3.11-py3.4-linux-x86_64.egg/yaml/parser.py", line 98, in check_event
  File "/usr/local/lib/python3.4/dist-packages/PyYAML-3.11-py3.4-linux-x86_64.egg/yaml/parser.py", line 428, in parse_block_mapping_key
  File "/usr/local/lib/python3.4/dist-packages/PyYAML-3.11-py3.4-linux-x86_64.egg/yaml/scanner.py", line 116, in check_token
  File "/usr/local/lib/python3.4/dist-packages/PyYAML-3.11-py3.4-linux-x86_64.egg/yaml/scanner.py", line 220, in fetch_more_tokens
  File "/usr/local/lib/python3.4/dist-packages/PyYAML-3.11-py3.4-linux-x86_64.egg/yaml/scanner.py", line 580, in fetch_value
yaml.scanner.ScannerError: mapping values are not allowed here
  in "./test1.yaml", line 3, column 93

1 个答案:

答案 0 :(得分:3)

冒号后不会遗漏任何空格,在input: /home/omnibrain/projects/company/data/data.csv开头的行中有太多空格。这就是为什么你看到第3行第93列

整行看起来像:

      input: /home/omnibrain/projects/company/data/data.csv                           output: some_data

它也应该有一些有趣的角色弄乱你的显示器通常你会看到一个字符串

...                           output: some_data

低于此处不允许的映射。

如果文件看起来相同,通常会发生这种差异,但实际上并非如此。从一个终端复制并粘贴到另一个终端后。或者在粘贴到像YAMLlint这样的网站之后。

在两个系统上为文件生成md5sum,以检查它们是否真的相同。在YAML文件上使用od -c检查它是否有奇怪的字符。