Reputation: 147
I have been researching a way to parse an xml document with more than one root in python but have been unsuccessful. Does anyone know of any helpful sites to accomplish this or have any isight as to whether this can be done? I have the xml file and python code below. I get a 'NoneType' object has no attribute 'text' error in my read loop of my python code.
XML File:
<ThursdayDay12>
<event>
<title>Refrigerator/freezer</title>
<startHour>11</startHour>
<startMinute>00</startMinute>
<duration units = 'min'>780</duration>
<load units = 'W'>33.77</load>
<comment>
'HANOVER HANRT30C Model'
</comment>
</event>
<event>
<title>Temperature</title>
<startHour>7</startHour>
<startMinute>30</startMinute>
<duration units = 'min'>990</duration>
<load units = 'W'>3520</load>
<comment>
'Assume AC requirement for house is 1 TR=3.52 kW'
</comment>
</event>
<event>
<title>Indoor lighting</title>
<startHour>20</startHour>
<startMinute>00</startMinute>
<duration units = 'min'>240</duration>
<load units = 'W'>250</load>
<comment>
'LED lighting for 4 rooms'
</comment>
</event>
</ThursdayDay12>
<FridayDay13>
<event>
<title>TV</title>
<startHour>19</startHour>
<startMinute>30</startMinute>
<duration units = 'min'>150</duration>
<load units = 'W'>3.96</load>
<comment>
'VIZIO E28h-C1 model rated at 34.7 kWh/yr'
</comment>
</event>
<event>
<title>Heat water for showers</title>
<startHour>19</startHour>
<startMinute>30</startMinute>
<duration units = 'min'>150</duration>
<load units = 'W'>1385</load>
<comment></comment>
</event>
</FridayDay13>
</SD2017NominalEnergyUse>
Python Code:
import xml.etree.ElementTree as ET
tree = ET.parse('SD2017NominalEnergyUse.xml')
root = tree.getroot()
title, start, end, load, duration = [],[],[],[],[]
for child in root:
for grandchild in child:
title.append(child.find('title').text)
sh = int(child.find('startHour').text)
sm = int(child.find('startMinute').text)
duration.append(float(child.find('duration').text))
start.append(sh*60+sm)
end.append(start[-1] + duration[-1])
load.append(float(child.find('load').text))
P = 0.0
for i in range(len(root)):
P = P+load[i]
print(P)
Upvotes: 1
Views: 430