'float'对象不是可迭代的错误

时间:2016-09-11 00:39:26

标签: python xml list floating-point

我正在尝试获取一个变量,该变量读取一个给定长度的时间(也是值列表)的加载值(在xml文档中给出)。 t的列表是从值“start”到“end”,间隔为15分钟。基本上我想要的是我想要一个负载值打印其各自时间列表的整个长度。因此,如果它的时间列表是[0 15 30 45 60],那么我希望它的加载值在列表中重复5次。我正在运行的xml文档和代码如下所示,我的potW赋值中出现错误,说''float'对象不可迭代。“我知道这很多但有任何建议吗?

import xml.etree.ElementTree as ET 
import os

class WaterModel:

    def __init__(self,fn):

        self.tree = ET.parse(fn)
        self.root = self.tree.getroot()

        self.title, self.start, self.end, self.load, self.duration, self.Type = [],[],[],[],[],[]

        for child in self.root:
            self.title.append(child.find('title').text)

            sh = int(child.find('startHour').text)
            sm = int(child.find('startMinute').text)
            self.duration.append(int(child.find('duration').text))
            self.start.append(sh*60+sm)
            self.end.append(self.start[-1] + self.duration[-1])
            self.Type.append(child.find('Type').text)
            self.load.append(float(child.find('load').text))


    def Wp(self):

        greyW = 0  
        for i in range(len(self.root)):
            if self.Type[i] == 'greywater':
                greyW += self.load[i]*self.duration[i]

        t = range(self.start[i], self.end[i]+1, 15)  
        for i in range(len(self.root)):
            for j in range(len(t)):
                if self.Type[i] == 'potable':
                    potW = [a for a in self.load[j]]

fn = 'SD2017NominalWaterUse.xml'
a = WaterModel(fn)
b = a.Wp()
print(b) 

xml文件'SD2017NominalWaterUse.xml',它被读入上面的代码:

<WaterNominalDay>

<event>
<title>Evening Washer</title>
<Type>greywater</Type>
<startHour>19</startHour>
<startMinute>30</startMinute>
<duration units = 'min'>270</duration>
<load units = 'gal/min'>.051852</load>
<comment>
''
</comment>
</event> 

<event>
<title>Morning Cooking</title>
<Type>potable</Type>
<startHour>7</startHour>
<startMinute>30</startMinute>
<duration units = 'min'>180</duration>
<load units = 'gal/min'>.003331</load>
<comment>
'5 lb water is .5995 gal'
</comment> 
</event>

<event>
<title>Evening Cooking</title>
<Type>potable</Type>
<startHour>19</startHour>
<startMinute>30</startMinute>
<duration units = 'min'>180</duration>
<load units = 'gal/min'>.003331</load>
<comment>
'5 lb water is .5995 gal'
</comment> 
</event>


<event>
<title>Leaks</title>
<Type>potable</Type>
<startHour>0</startHour>
<startMinute>00</startMinute>
<duration units = 'min'>1440</duration>
<load units = 'gal/min'>.006944</load>
<comment>
''
</comment> 
</event>

</WaterNominalDay>

1 个答案:

答案 0 :(得分:0)

self.load我是一个float的数组,所以self.load[j]将是一个浮点数,这就是你想要迭代的东西;因此错误信息。