yaml parse in python: SyntaxError: invalid syntax -


i want parse yaml file in python. hosts.yaml file below:

access:   host: abc     hosturl: url-for-abc     bearertoken: jjlhlj     expires: somedate   host: xyz     hosturl: url-for-xyz     bearertoken: kdsddh     expires: somedate 

my python code parse yaml below:

import yaml  def parse():     open('hosts.yaml', 'r') hosts_file:         hosts = yaml.load(hosts_file)         host, val in  hosts["access"]["host"]:             print host             print host["hosturl"]             print host["bearertoken"]  parse() 

i getting below error:

python parse_yaml.py traceback (most recent call last):   file "parse_yaml.py", line 19, in <module>     parse()   file "parse_yaml.py", line 13, in parse2     hosts = yaml.safe_load(hosts_file)   file "/usr/lib64/python2.7/site-packages/yaml/__init__.py", line 93, in safe_load     return load(stream, safeloader)   file "/usr/lib64/python2.7/site-packages/yaml/__init__.py", line 71, in load     return loader.get_single_data()   file "/usr/lib64/python2.7/site-packages/yaml/constructor.py", line 37, in get_single_data     node = self.get_single_node()   file "/usr/lib64/python2.7/site-packages/yaml/composer.py", line 36, in get_single_node     document = self.compose_document()   file "/usr/lib64/python2.7/site-packages/yaml/composer.py", line 55, in compose_document     node = self.compose_node(none, none)   file "/usr/lib64/python2.7/site-packages/yaml/composer.py", line 84, in compose_node     node = self.compose_mapping_node(anchor)   file "/usr/lib64/python2.7/site-packages/yaml/composer.py", line 133, in compose_mapping_node     item_value = self.compose_node(node, item_key)   file "/usr/lib64/python2.7/site-packages/yaml/composer.py", line 84, in compose_node     node = self.compose_mapping_node(anchor)   file "/usr/lib64/python2.7/site-packages/yaml/composer.py", line 127, in compose_mapping_node     while not self.check_event(mappingendevent):   file "/usr/lib64/python2.7/site-packages/yaml/parser.py", line 98, in check_event     self.current_event = self.state()   file "/usr/lib64/python2.7/site-packages/yaml/parser.py", line 428, in parse_block_mapping_key     if self.check_token(keytoken):   file "/usr/lib64/python2.7/site-packages/yaml/scanner.py", line 116, in check_token     self.fetch_more_tokens()   file "/usr/lib64/python2.7/site-packages/yaml/scanner.py", line 220, in fetch_more_tokens     return self.fetch_value()   file "/usr/lib64/python2.7/site-packages/yaml/scanner.py", line 576, in fetch_value     self.get_mark()) yaml.scanner.scannererror: mapping values not allowed here   in "hosts.yaml", line 3, column 12 

i not able understand, if wrong yaml file structure of issue code. in advance.

you cannot have both string , object value of key. can either strings

access:   host: abc   host: xyz 

or objects

access:   host:     hosturl: url-for-abc     bearertoken: jjlhlj     expires: somedate   host:     hosturl: url-for-xyz     bearertoken: kdsddh     expires: somedate 

but not both

access:   host: abc     hosturl: url-for-abc     bearertoken: jjlhlj     expires: somedate   host: xyz     hosturl: url-for-xyz     bearertoken: kdsddh     expires: somedate 

i suggest put name of host in name key in object

access:   host:     name: abc     hosturl: url-for-abc     bearertoken: jjlhlj     expires: somedate   host:     name: xyz     hosturl: url-for-xyz     bearertoken: kdsddh     expires: somedate 

Comments

Popular posts from this blog

Sort a complex associative array in PHP -

vb.net - How to ignore if a cell is empty nothing -

recursion - Can every recursive algorithm be improved with dynamic programming? -