我目前正在获得一个扭曲的平均值。我是平均项目,但如果我看到某个产品名称,我需要将该值计为3.我无法使其正常工作。下面是我的代码和XML(缩短)文件。基本上如果循环看到Tofus,数量应该被3覆盖而不是9 ..然后得到平均值。
import xml.etree.ElementTree as ET
root = ET.ElementTree(file="nwind_medium.xml")
orders = root.findall("./orders")
quantityTotal = 0
for order in orders:
orderdetails = order.findall("./orderdetails")
for detail in orderdetails:
productName = detail.findall("./products/productname")
quantityNew = detail.findall("./quantity")
if productName[0].text == "chang" or productName[0].text == "chai" or productName[0].text == "Tofus":
quantityNew = 3;
else:
quantityTotal += float(quantityNew[0].text)
numberQuantity = len(orders)
print"订单的平均订单数",round((quantityTotal / numberQuantity),1)
<?xml version="1.0"?>
-<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>
答案 0 :(得分:0)
import xml.etree.ElementTree as ET
root = ET.ElementTree(file="nwind_medium.xml")
orders = root.findall("./orders")
quantityTotal = 0
for order in orders:
orderdetails = order.findall("./orderdetails")
for detail in orderdetails:
productName = detail.findall("./products/productname")
quantityNew = detail.findall("./quantity")
if productName[0].text == "chang" or productName[0].text == "chai" or productName[0].text == "Tofus":
_quantity = 3
else:
_quantity = float(quantityNew[0].text)
quantityTotal += _quantity
numberQuantity = len(orders)