如何使用pyyaml解析集合并检索值

时间:2019-01-15 17:46:33

标签: python-3.x pyyaml

我能够使一个简单,直接的YAML解析示例正常工作,但是我在解析真正的YAML文件的嵌套结构时遇到了问题。我在遍历集合时遇到问题。我正在使用python3.6,这是下面的基本代码片段。

import yaml                                                    
with open('action.yml', 'r') as action:                        
    doc = yaml.load(action)                                    
    action = doc["actions"]["a1"]["action"]                    
    print (action)                                             
    tval = doc["actions"]["a1"]["add"]["filters[1]"]["value"]  
    print (tval)                                               
print ('done')

运行时错误如下:

Traceback (most recent call last):                                  
  File "actionparser.py", line 27, in <module>                      
    tval = doc["actions"]["a1"]["add"]["filters[1]"]["value"]       
KeyError: 'filters[1]'                                              

真正的YAML输入文件如下所示。如何获取动作:a1:添加:过滤器[1]:值和动作:a2:添加:过滤器[1]:值

--- 
actions: 
  a1: 
    action: alias
    add: 
      filters: 
        - 
          filtertype: pattern
          kind: prefix
          value: logstash-
        - 
          filtertype: pattern
          kind: regex
          value: ^logstash-dev-firehbridge-cold    <== How do I get this?
        - 
          filtertype: pattern
          kind: timestring
          unit_count: 2
    description: "Add/Remove alias1"
    options: 
      disable_action: false
      ignore_empty_list: true
      name: current_firebridge
      warn_if_no_indices: true
    remove: 
      filters: 
        - 
          filtertype: pattern
          kind: prefix
          value: logstash-
        - 
          filtertype: pattern
          kind: regex
          value: ^logstash-dev-firehbridge-cold
        - 
          filtertype: pattern
          kind: timestring
          unit_count: 2
          value: "%Y.%m.%d"
  a2: 
    action: alias
    add: 
      filters: 
        - 
          filtertype: pattern
          kind: prefix
          value: logstash-
        - 
          filtertype: pattern
          kind: regex
          value: ^logstash-dev-firehbridge-hot    <== and how do I get this?
        - 
          filtertype: pattern
          kind: timestring
          unit_count: 2
    description: "Add/Remove alias2"
    options: 
      disable_action: false
      ignore_empty_list: true
      name: current_firebridge
      warn_if_no_indices: true
    remove: 
      filters: 
        - 
          filtertype: pattern
          kind: prefix
          value: logstash-
        - 
          filtertype: pattern
          kind: regex
          value: ^logstash-dev-firehbridge-hot
        - 
          filtertype: pattern
          kind: timestring
          unit_count: 2
          value: "%Y.%m.%d"

1 个答案:

答案 0 :(得分:1)

首先:PyYAML的默认.safe_load()有潜在的危险, 您几乎永远不需要它。请改用['somekey']

加载后,您可以使用python [somenumber]访问任何映射键, 键查找语法以及使用列表项索引(input.yaml)的任何序列。

假设您的文件名为<== How do I get this?(删除了import yaml with open('input.yaml') as fp: data = yaml.safe_load(fp) print(data['actions']['a1']['add']['filters'][1]['value']) print(data['actions']['a2']['add']['filters'][1]['value'])

^logstash-dev-firehbridge-cold
^logstash-dev-firehbridge-hot

给出:

<table aria-hidden="true" cellspacing="0" cellpadding="0" border="0" width="430" style="margin: 0; max-width: 100%;">
  <tr>
   <td style="padding: 0px;">
     <img src="https://uploaddeimagens.com.br/images/001/834/894/original/pic.png?1547564265" width="430" alt="" style="display: block; margin: 0; max-width: 100%;" />
   </td>
 </tr>
</table>
<table role="contentinfo" cellspacing="0" cellpadding="0" border="0" width="430" style="margin: 0; max-width: 100%;">
  <tr>
   <td valign="top" style="padding: 10px;">
   <p style="font-size: 14px; font-family: Arial, sans-serif; color: #464646; margin: 0;">
   <strong>Company</strong><br />
   <strong>Role</strong><br /><br />
  
   <strong>E-mail:</strong> <a href="mailto: aaa@gmail.com" target="_blank" style="color: #000000; text-decoration: none;">aaa@gmail.com</a><br />
   <strong>Phone:</strong> <span class="color: #000000; text-decoration: none;">1231231231</span>
   <strong>| Phone:</strong> <span class="color: #000000; text-decoration: none;">2222123123</span><br />
   <strong>Fax:</strong> <span class="color: #000000; text-decoration: none;">00010102301</span><br /><br />

   <img src="https://img.icons8.com/color/50/000000/domain.png" width="30" height="30" border="1" style="display: inline-block;" /	>
   <img src="https://img.icons8.com/color/50/000000/facebook.png" width="30" height="30" border="1" style="display: inline-block;" />
  </p>
</td>
<td valign="top" style="padding: 10px 0;">
  <img src="https://via.placeholder.com/100x100" width="100" style="height: auto; display: block; margin: 0;" />
</td>
  </tr>
</table>
<table aria-hidden="true" cellspacing="0" cellpadding="0" border="0" width="430" style="margin: 0; max-width: 100%;">
  <tr>
   <td style="padding: 0px;">
   <img src="https://uploaddeimagens.com.br/images/001/834/900/original/pic-2.png?1547564365" width="430" alt="" style="display: block; margin: 0; max-width: 100%;" />
   </td>
  </tr>
</table>