我有一个小的XML片段,如下所示。我想得到:
我需要将这些值显示为表,因此必须使用组名的相应表达式。
我们是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
。是否可以将这两个数据作为字典中的键值对提取?
无需转换为字典或列表。只需使用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
的expression
元素。
它同样容易通过所有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])