我想获得一个变量,读取一个给定的时间长度(也是一个值列表)的负载值(在XML文档中给出)。 t的列表是从“开始”到“结束”的值,间隔为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>
是不是'[a for a X]'只是'X'的浅拷贝?不会'X [:]'完成同样的事情(如果这是你想要做的)? –