将XML数据读入字典

问题描述 投票:0回答:1

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

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

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

我们是CML鱼片:

<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。是否可以将这两个数据作为字典中的键值对提取?

python python-3.x xml-parsing elementtree
1个回答
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])

这只选择实际上有groupfilterexpression元素。

它同样容易通过所有groupfilter元素:

for group in tree.iterfind('.//group'):
    for groupfilter in group.iterfind('.//groupfilter'):
        groupname = group.get('name')
        expression = groupfilter.get('expression', '')  # default: empty string

        print([groupname, expression])
© www.soinside.com 2019 - 2024. All rights reserved.