23

I am on ubunty 64 with python 2.7 and using PyYAML-3.10

Below is my yaml file:

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

When I run the below code I get the below error. But if I remove the lines above the treeroot the code works:

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
kenm
  • 23,127
  • 2
  • 43
  • 62

6 Answers6

55

Try putting spaces after the colons.

kenm
  • 23,127
  • 2
  • 43
  • 62
  • 17
    A simple way to improve your answer would be to show the correctly formatted YAML file. – Tom Fenech May 18 '17 at 14:00
  • 5
    I don't think it's just a matter of collon. I have the issue here while having after the collon... https://stackoverflow.com/questions/50914422/parse-aws-cloudformation-template-with-yaml-library – nixmind Jun 19 '18 at 15:40
  • 4
    The error may also appear when you have wrong indentation in line. That was my case. – robson Oct 08 '19 at 11:57
27

For anyone who comes here and finds that even if they have spaces after the colon, they still get this error

You can also get this error if you copy the yaml text from some formatted source (for me it was a Slack message). This will invisibly swap in non-ASCII characters that the standard YAML reader can't read, but which look the same.

Solution is to only copy from raw, ASCII source.

Peter
  • 12,274
  • 9
  • 71
  • 86
8

If someone comes here and has formatting and spaces right but the error persists.

Check if there is colon after the version! (me facepalming duh)

Error:

version '3.7'

services:
  rabbitmq3:
    image: rabbitmq:3-management

Fixed:

version: '3.7'

services:
  rabbitmq3:
    image: rabbitmq:3-management
Victor Martins
  • 1,355
  • 1
  • 12
  • 23
6

yaml files do not accept values immediately after the colon mark in the file content. Enter the value after a space, save the file and run again, the error will be gone. I had encountered the similar error during my automation using BDD, and got this fixed after a lot of debugging.

Suprith
  • 61
  • 1
  • 4
  • 2
    I don't think it's just a matter of collon. I have the issue here while having after the collon... https://stackoverflow.com/questions/50914422/parse-aws-cloudformation-template-with-yaml-library – nixmind Jun 19 '18 at 15:27
1

I also get the same error when a section isnt indented correctly. In my case the lower line was over-indented.

Wrong:

tags:
  - name: my_tag
parameters:
  - in: path
        name: id

corrected:

tags:
  - name: my_tag
parameters:
  - in: path
    name: id

Sadly, this is recognized as a "mapping values" error as well.

c8999c 3f964f64
  • 1,430
  • 1
  • 12
  • 25
0

After trying all the solutions above, I still got the same error. What worked for me was to create a .yml file directly from vscode and copy the content to that file and it worked!!!

Masih
  • 920
  • 2
  • 19
  • 36