将XML数据读入字典

时间:2018-03-02 09:30:53

标签: python python-3.x xml-parsing elementtree

我有一个小的XML片段,如下所示。我想得到:

  • groupname属性
  • groupfilter表达式属性,对应于groupname

我需要将这些值显示为表格,因此组名称的相应表达式是必须的。

我的XML文件:

<datas xmlns:user="http://tempuri.org">
  <data>
    <group name='[Action (Market Size,Market,Product Type,Product)]' name-style='unqualified' user:auto-column='sheet_link'>
      <groupfilter function='crossjoin'>
        <groupfilter function='level-members' level='[Market Size]' />
        <groupfilter function='level-members' level='[Market]' />
        <groupfilter function='level-members' level='[Product Type]' />
        <groupfilter function='level-members' level='[Product]' />
      </groupfilter>
    </group>
    <group name='[Action (Market,Product Type,Product)]' name-style='unqualified' user:auto-column='sheet_link'>
      <groupfilter function='crossjoin'>
        <groupfilter function='level-members' level='[Market]' />
        <groupfilter function='level-members' level='[Product Type]' />
        <groupfilter function='level-members' level='[Product]' />
      </groupfilter>
    </group>
     <group name='[Set 1 sanu]' name-style='unqualified' user:ui-builder='filter-group'>
      <groupfilter expression='SUM([COGS]) == 0.' function='filter' user:ui-filter-by-field='true' user:ui-marker='filter-by'>
        <groupfilter function='level-members' level='[Product Line]' user:ui-enumeration='all' user:ui-marker='enumerate' />
      </groupfilter>
    </group>
    <group name='[Action (Category,Region,Sub-Category (group))]' name-style='unqualified' user:auto-column='sheet_link'>
      <groupfilter function='crossjoin'>
        <groupfilter function='level-members' level='[Product Category]' />
        <groupfilter function='level-members' level='[Region]' />
        <groupfilter function='level-members' level='[Sub-Category (group)]' />
      </groupfilter>
    </group>
    <group caption='Top Customers by Profit' name='[Set 1]' name-style='unqualified' user:ui-builder='filter-group'>
      <groupfilter count='[Parameters].[Parameter 2]' end='top' function='end' units='records' user:ui-marker='end' user:ui-top-by-field='true'>
        <groupfilter direction='DESC' expression='SUM([Profit])' function='order' user:ui-marker='order'>
          <groupfilter function='level-members' level='[Customer Name]' user:ui-enumeration='all' user:ui-marker='enumerate' />
        </groupfilter>
      </groupfilter>
    </group>
  </data>
</datas>

我的尝试:

root1是etree将xml解析为文件的根元素)

for setvar in root1.findall('.//group'):
    setname.append(setvar.get('name'))
    groupfilter = setvar.findall('.//groupfilter')
    if groupfilter:
        for i in groupfilter:
            if i.get('expression'):
                setexpression.append(i.get('expression'))
             else:
                 setexpression.append('')

要将结果显示为表格,我将它们附加到list。是否可以将这两个数据作为字典中的键值对提取出来?

1 个答案:

答案 0 :(得分:1)

无需转换为字典或列表。只需使用XML树作为基础数据结构。

for group in tree.iterfind('.//group'):
    for groupfilter in group.iterfind('.//groupfilter[@expression]'):
        groupname = group.get('name')
        expression = groupfilter.get('expression')

        print([groupname, expression])

这仅选择实际拥有groupfilter的{​​{1}}元素。

通过所有expression元素同样容易:

groupfilter