Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python元素树-获得平均值_Python - Fatal编程技术网

python元素树-获得平均值

python元素树-获得平均值,python,Python,在python中使用元素树,我想得到一个平均值 下面是我的数据 Order A has a quantity of 12,10,and 5.. total is 27 Order B has a quantity of 9 and 40... total is 49 Order C has a quantity of 10,35, and 15.. total is 60 当你把它们相加,然后除以3,我应该得到45.33。但根据我下面的代码,我得到了20分:(我正在从一个XML文件中提取上

在python中使用元素树,我想得到一个平均值

下面是我的数据

Order A has a quantity of 12,10,and 5.. total is 27
Order B has a quantity of 9 and 40... total is 49
Order C has a quantity of 10,35, and 15.. total is 60  
当你把它们相加,然后除以3,我应该得到45.33。但根据我下面的代码,我得到了20分:(我正在从一个XML文件中提取上述数据。您能帮我确定代码中的问题吗?谢谢

import xml.etree.ElementTree as ET
root = ET.ElementTree(file="nwind_medium.xml")

orders = root.findall("./orders")
for order in orders:
    orderdetails = order.findall("./orderdetails")
    total = 0
    for detail in orderdetails:
        quantity = detail.findall("./quantity")
        total += float(quantity[0].text)
numberOrders = len(orders)

print "The average number of itmes in order is", round((total / numberOrders),2)
以下是整个XML文件(已更新)
-酒和酒骑士葡萄园——72支吉奥瓦尼马苏里拉酒34.8 5-14支福提尼酒s.r.l.——11支克索卡布拉酒14 12-5支卡布拉斯合作酒——42支新加坡福建炒面酒9.8 10-20支利卡酒交易——汤姆酒——14支豆腐18.6 9-6支马尤米酒——51支曼吉木普干苹果42.4 40-24克/天,伙计-哈纳里·卡恩斯·哈纳-65路易斯安那火辣辣椒酱16.8 15-2新奥尔良卡琼美食-41杰克新英格兰蛤蜊浓汤7.7 10-19新英格兰海鲜罐头厂-51曼吉姆普干苹果42.4 35-24克/天,伙计你通过订单重置每次迭代的总数。如果你需要所有命令中的一个移动

total = 0

在外部循环之前。

@suer\u new\u在python中您可以提供更可读的xml格式吗?我可以将其粘贴到原始问题上吗?什么是
数量[0].text
每次通过循环时?@user\u new\u在\u python中,您的XML被破坏,除了有趣的
-
+
the@user_new_in_python还没有。我指的是XML,它可以复制到文本编辑器中,保存并验证为有效的XML。否“-”或“+”,但所有元素都已关闭。您可以删除所有元素,这些元素不是显示问题所必需的,但您应提供有效的XML。耶…这很简单。谢谢you@user_new_in_python,如果某个答案解决了你的问题,请不要忘记投赞成票。欢迎这么做!嗨,你指的是复选标记吗?我已经这么做了。