来自XML的python动态字典

时间:2015-07-09 12:13:11

标签: python dictionary

我在XML文件中有一段如下:

<p1:car>                           
    <p1:feature car="111" type="color">511</p1:feature>
    <p1:feature car="223" type="color">542</p1:feature>
    <p1:feature car="299" type="color">559</p1:feature>
    <p1:feature car="323" type="color">564</p1:feature>
    <p1:feature car="353" type="color">564</p1:feature>
    <p1:feature car="391" type="color">570</p1:feature>
    <p1:feature car="448" type="color">570</p1:feature>

    <p1:feature car="111" type="tires" unit="percent">511</p1:feature>
    <p1:feature car="223" type="tires" unit="percent">513</p1:feature>
    <p1:feature car="299" type="tires" unit="percent">516</p1:feature>
    <p1:feature car="323" type="tires" unit="percent">516</p1:feature>
    <p1:feature car="353" type="tires" unit="percent">518</p1:feature>
    <p1:feature car="391" type="tires" unit="percent">520</p1:feature>
    <p1:feature car="448" type="tires" unit="percent">520</p1:feature>
</p1:car>

我想要的是什么:

我正在使用LXML,我正在循环遍历每个元素和属性,并愿意以下列方式动态创建字典:

{color:[(111,511),(223,542).....] tires:[(111,511),(223,542).....]}

我尝试了什么:

from collections import defaultdict

a=[] #extract attributes and text to list
comp = defaultdict(list)

 #some code to access root element

for m in k.getchildren(): #this iterates through <p1:car> element
                a.append([m.get('type'), m.get('car'), m.text])
                comp[m[0]].append(m[1:])

运行此程序后,我收到此错误:

File "lxml.etree.pyx", line 1098, in lxml.etree._Element.__getitem__ (src\lxml\lxml.etree.c:47744)
IndexError: list index out of range

1 个答案:

答案 0 :(得分:1)

你应该在最后一行用[-1]替换m变量,或者(更好)使用一些临时变量来存储值:

for m in k.getchildren(): #this iterates through <p1:car> element
    record = [m.get('type'), m.get('car'), m.text]
    a.append(record)
    comp[record[0]].append(tuple(record[1:]))