1

I am trying to follow these two examples:

Parsing a YAML file in Python, and accessing the data?

YAML parsing and Python?

but for some reason I keep getting the following error message:

Traceback (most recent call last):
  File "test.py", line 3, in <module>
    doc = yaml.load(f)
  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 55, in compose_document
    node = self.compose_node(None, None)
  File "/usr/local/lib/python2.7/dist-packages/yaml/composer.py", line 84, in compose_node
    node = self.compose_mapping_node(anchor)
  File "/usr/local/lib/python2.7/dist-packages/yaml/composer.py", line 133, in compose_mapping_node
    item_value = self.compose_node(node, item_key)
  File "/usr/local/lib/python2.7/dist-packages/yaml/composer.py", line 64, in compose_node
    if self.check_event(AliasEvent):
  File "/usr/local/lib/python2.7/dist-packages/yaml/parser.py", line 98, in check_event
    self.current_event = self.state()
  File "/usr/local/lib/python2.7/dist-packages/yaml/parser.py", line 449, in parse_block_mapping_value
    if not self.check_token(KeyToken, ValueToken, BlockEndToken):
  File "/usr/local/lib/python2.7/dist-packages/yaml/scanner.py", line 116, in check_token
    self.fetch_more_tokens()
  File "/usr/local/lib/python2.7/dist-packages/yaml/scanner.py", line 257, in fetch_more_tokens
    % ch.encode('utf-8'), self.get_mark())
yaml.scanner.ScannerError: while scanning for the next token
found character '\t' that cannot start any token
  in "tree.yaml", line 2, column 1

I have installed PyYAML by doing the following:

python setup.py install

I also tested it with:

python setup.py test

and it seemed to be ok.

Now granted I am a little new to Python and YAML, but I have followed those two links to the letter. I have a file named tree.yaml and a file named test.py each containing the following:

test.py

import yaml
with open('tree.yaml', 'r') as f:
    doc = yaml.load(f)

txt = doc["treeroot"]["branch1"]
print txt

tree.yaml

treeroot:
        branch1: branch1 text
        branch2: branch2 text

and before you ask, yes I verified my tab space and I ran the YAML file through a validator. Any idea why this isnt working? Seems like its not able to communicate with PyYAML. Again I am very new to all of this, so I don't understand yet how it works and how everything integrates.

Thanks for the help!

I'm heading home for the day so will be away from computer for a little bit. Will check back in soon or will try to reply/answer any questions via phone.

Thanks

Community
  • 1
  • 1
Atomiklan
  • 5,164
  • 11
  • 40
  • 62

1 Answers1

1

Yaml file uses spaces , not tabs as an indentation. In the error you provided it is clearly says that the first character in the second line is tab. Please replace it with 2 or 4 spaces and it should work.

Tabs are not permited in yaml since they are treated differently by different editors and tools. And to avoid this confusion you should be using spaces.

Vor
  • 33,215
  • 43
  • 135
  • 193