python changing the value while doing average -
i'm getting average value twist. i'm averaging items if see product name, need count value 3. can't make work. below code , xml (shortened) file. if loop see tofus, quantity should overwritten 3 , not 9..then average.
import xml.etree.elementtree et root = et.elementtree(file="nwind_medium.xml") orders = root.findall("./orders") quantitytotal = 0 order in orders: orderdetails = order.findall("./orderdetails") 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 "average number of items order is", 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>
import xml.etree.elementtree et root = et.elementtree(file="nwind_medium.xml") orders = root.findall("./orders") quantitytotal = 0 order in orders: orderdetails = order.findall("./orderdetails") 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)
Comments
Post a Comment