2017-09-11 100 views
-1

我想解析python中的yaml文件。我hosts.yaml文件如下:yaml在python中解析:SyntaxError:無效的語法

access: 
    host: abc 
    hostUrl: url-for-abc 
    bearerToken: jjlhlj 
    expires: someDate 
    host: xyz 
    hostUrl: url-for-xyz 
    bearerToken: kdsddh 
    expires: someDate 

我的Python代碼來解析YAML是如下:

import yaml 

def parse(): 
    with open('hosts.yaml', 'r') as hosts_file: 
     hosts = yaml.load(hosts_file) 
     for host, val in hosts["access"]["host"]: 
      print host 
      print host["hostUrl"] 
      print host["bearerToken"] 

parse() 

我收到以下錯誤:

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 are not allowed here 
    in "hosts.yaml", line 3, column 12 

我無法理解,如果yaml文件結構與代碼有些問題是錯誤的。 在此先感謝。

+0

您運行的代碼不是與您顯示的代碼相同。另外,ScannerError不是一個SyntaxError。 –

+0

是的......其實在代碼中有一些官方價值。我在這裏複製時進行編輯。 –

+0

絕對沒有必要使用PyYAML的'load()',這被證明是不安全的。 – Anthon

回答

2

您不能同時擁有一個字符串和一個對象作爲鍵的值。您可以做字符串

access: 
    host: abc 
    host: xyz 

或物體

access: 
    host: 
    hostUrl: url-for-abc 
    bearerToken: jjlhlj 
    expires: someDate 
    host: 
    hostUrl: url-for-xyz 
    bearerToken: kdsddh 
    expires: someDate 

但不能同時

access: 
    host: abc 
    hostUrl: url-for-abc 
    bearerToken: jjlhlj 
    expires: someDate 
    host: xyz 
    hostUrl: url-for-xyz 
    bearerToken: kdsddh 
    expires: someDate 

我建議你在對象將主機名在name關鍵

access: 
    host: 
    name: abc 
    hostUrl: url-for-abc 
    bearerToken: jjlhlj 
    expires: someDate 
    host: 
    name: xyz 
    hostUrl: url-for-xyz 
    bearerToken: kdsddh 
    expires: someDate 
+0

謝謝。但是,如果兩者都是主機,我如何迭代它以獲取嵌套節點的值(名稱,到期等)? –

+0

完全不同的問題。 –