gpt4 book ai didi

python - 在python中解析具有多个孙子的xml文档

转载 作者:太空宇宙 更新时间:2023-11-03 16:18:15 27 4
gpt4 key购买 nike

我一直在研究一种在 python 中解析具有多个根的 xml 文档的方法,但没有成功。有谁知道有什么有用的网站可以完成此任务,或者有任何关于是否可以做到这一点的见解?我有下面的 xml 文件和 python 代码。我在 python 代码的读取循环中收到“NoneType”对象没有属性“文本”错误。

XML 文件:

<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 代码:

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)

最佳答案

你试过吗xml.domminidom

关于python - 在python中解析具有多个孙子的xml文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38724153/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com