Elementtree和反查找相应的值

时间:2014-06-27 05:48:17

标签: python

我有一个带有计数器的元素树python代码。我需要获取具体名称(产品名称,供应商名称和供应商ID)。 下面的代码为我提供了销售量最大的产品ID。现在我需要获取相应产品ID的产品名称,供应商名称和供应商ID

orders = root.findall("./orders")
total ={}
for order in orders:
    orderdetails = order.findall("./orderdetails")
    for detail in orderdetails:
        productid = detail.findall("./products/productid")[0].text
        quantity = detail.findall("./quantity")
        if productid in total.keys():
            total[productid]+=float(quantity[0].text)
        else:
            print productid,float(quantity[0].text)
            print total
            total[productid ]=float(quantity[0].text)


print Counter(total).most_common(1)[0][0]

<小时/>     这是XML     

-<nwind>


-<orders another="Friday" orderid="10248">


-<customers>

<companyname>Vins et alcools Chevalier</companyname>

<customerid>VINET</customerid>

</customers>


-<orderdetails>


-<products>

<productid>72</productid>

<productname>Mozzarella di Giovanni</productname>

</products>

<unitprice>34.8</unitprice>

<quantity>5</quantity>


-<suppliers>

<supplierid>14</supplierid>

<companyname>Formaggi Fortini s.r.l.</companyname>

</suppliers>

</orderdetails>


-<orderdetails>


-<products>

<productid>11</productid>

<productname>Queso Cabrales</productname>

</products>

<unitprice>14</unitprice>

<quantity>12</quantity>


-<suppliers>

<supplierid>5</supplierid>

<companyname>Cooperativa de Quesos 'Las Cabras'</companyname>

</suppliers>

</orderdetails>


-<orderdetails>


-<products>

<productid>42</productid>

<productname>Singaporean Hokkien Fried Mee</productname>

</products>

<unitprice>9.8</unitprice>

<quantity>10</quantity>


-<suppliers>

<supplierid>20</supplierid>

<companyname>Leka Trading</companyname>

</suppliers>

</orderdetails>

</orders>


-<orders orderid="10249">


-<customers>

<companyname>Toms Spezialitaten</companyname>

<customerid>TOMSP</customerid>

</customers>


-<orderdetails>


-<products>

<productid>14</productid>

<productname>Tofus</productname>

</products>

<unitprice>18.6</unitprice>

<quantity>9</quantity>


-<suppliers>

<supplierid>6</supplierid>

<companyname>Mayumi's</companyname>

</suppliers>

</orderdetails>


-<orderdetails>


-<products>

<productid>51</productid>

<productname>Manjimup Dried Apples</productname>

</products>

<unitprice>42.4</unitprice>

<quantity>40</quantity>


-<suppliers>

<supplierid>24</supplierid>

<companyname>G'day, Mate</companyname>

</suppliers>

</orderdetails>

</orders>


-<orders orderid="10250">


-<customers>

<companyname>Hanari Carnes</companyname>

<customerid>HANAR</customerid>

</customers>


-<orderdetails>


-<products>

<productid>65</productid>

<productname>Louisiana Fiery Hot Pepper Sauce</productname>

</products>

<unitprice>16.8</unitprice>

<quantity>15</quantity>


-<suppliers>

<supplierid>2</supplierid>

<companyname>New Orleans Cajun Delights</companyname>

</suppliers>

</orderdetails>


-<orderdetails>


-<products>

<productid>41</productid>

<productname>Jack's New England Clam Chowder</productname>

</products>

<unitprice>7.7</unitprice>

<quantity>10</quantity>


-<suppliers>

<supplierid>19</supplierid>

<companyname>New England Seafood Cannery</companyname>

</suppliers>

</orderdetails>


-<orderdetails>


-<products>

<productid>51</productid>

<productname>Manjimup Dried Apples</productname>

</products>

<unitprice>42.4</unitprice>

<quantity>35</quantity>


-<suppliers>

<supplierid>24</supplierid>

<companyname>G'day, Mate</companyname>

</suppliers>

</orderdetails>

</orders>

</nwind>

1 个答案:

答案 0 :(得分:-1)

import xml.etree.ElementTree as ET
from collections import Counter,defaultdict

root = ET.ElementTree(file="nwind_medium.xml")

orders = root.findall("./orders")
total = {}
# intalize the default dict here
productdetails=defaultdict(list,{})
for order in orders:
    orderdetails = order.findall("./orderdetails")
    for detail in orderdetails:
        productid = detail.findall("./products/productid")[0].text
        quantity = detail.findall("./quantity")
        supplierid = detail.findall("./suppliers/supplierid")[0].text
        companyname = detail.findall("./suppliers/companyname")[0].text
        productname = detail.findall("./products/productname")[0].text
        #use append to add the details into the product details
        productdetails[productid].append((supplierid,companyname,productname))
        if productid in total.keys():
            total[productid]+=float(quantity[0].text)
        else:


            total[productid]=float(quantity[0].text)

maxprod= Counter(total).most_common(1)[0][0]
print set(productdetails[maxprod])

说明:

  1. 由于相同的产品ID具有不同的产品名称,供应商名称,ID以实现我们需要利用(在此学习)defaultdict
  2. 然后将产品ID提供给产品详细信息您将获得所需的详细信息