1. ホーム
  2. python

[解決済み】PythonとPYAML - yaml.scanner.ScannerError: マッピングの値はここでは許可されません。

2022-02-04 20:11:28

質問

私はubunty 64でPython 2.7を使い、PyYAML-3.10を使用しています。

以下は私のyamlファイルです。

host:localhost
username:root
password:test
database:test
operations_database:operations
treeroot:
    branch1:
        name: Node 1
        branch1-1:
            name: Node 1-1
    branch2:
        name: Node 2
        branch2-1:
            name: Node 2-1

以下のコードを実行すると、以下のエラーが表示されます。 しかし、treerootの上の行を削除すると、コードは動作します。

from yaml import load, dump
try:
    from yaml import CLoader as Loader, CDumper as Dumper
except ImportError:
    from yaml import Loader, Dumper
f=open('amazon.yaml')  
data = load(f, Loader=Loader) 

Traceback (most recent call last):
  File "/home/ubuntu/workspace/Amazon-Products-Crawler-1/config_files/test_yaml.py", line 10, in <module>
    data = load(f, Loader=Loader) 
  File "/usr/local/lib/python2.7/dist-packages/yaml/__init__.py", line 71, in load
    return loader.get_single_data()
  File "/usr/local/lib/python2.7/dist-packages/yaml/constructor.py", line 37, in get_single_data
    node = self.get_single_node()
  File "/usr/local/lib/python2.7/dist-packages/yaml/composer.py", line 36, in get_single_node
    document = self.compose_document()
  File "/usr/local/lib/python2.7/dist-packages/yaml/composer.py", line 58, in compose_document
    self.get_event()
  File "/usr/local/lib/python2.7/dist-packages/yaml/parser.py", line 118, in get_event
    self.current_event = self.state()
  File "/usr/local/lib/python2.7/dist-packages/yaml/parser.py", line 193, in parse_document_end
    token = self.peek_token()
  File "/usr/local/lib/python2.7/dist-packages/yaml/scanner.py", line 128, in peek_token
    self.fetch_more_tokens()
  File "/usr/local/lib/python2.7/dist-packages/yaml/scanner.py", line 220, in fetch_more_tokens
    return self.fetch_value()
  File "/usr/local/lib/python2.7/dist-packages/yaml/scanner.py", line 580, in fetch_value
    self.get_mark())
yaml.scanner.ScannerError: mapping values are not allowed here
  in "amazon.yaml", line 6, column 9

解決方法は?

コロンの後にスペースを入れてみてください。