gpt4 book ai didi

python - python中的XML解析(Elia结构)

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

我想解析这个 xml 类型的文件:

<?xml version="1.0" encoding="utf-8"?>
<SolarForecastingChartDataForZone xmlns="http://schemas.datacontract.org/2004/07/Elia.PublicationService.DomainInterface.SolarForecasting.v3" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<ErrorMessage i:nil="true"/>
<IntervalInMinutes>15</IntervalInMinutes>
<SolarForecastingChartDataForZoneItems>
<SolarForecastingChartDataForZoneItem>
<DayAheadForecast>-50</DayAheadForecast>
<DayAheadP10>-50</DayAheadP10>
<DayAheadP90>-50</DayAheadP90>
<Forecast>0</Forecast>
<ForecastP10>0</ForecastP10>
<ForecastP90>0</ForecastP90>
<ForecastUpdated>0</ForecastUpdated>
<IntraDayP10>-50</IntraDayP10>
<IntraDayP90>-50</IntraDayP90>
<LoadFactor>0</LoadFactor>
<RealTime>0</RealTime>
<StartsOn xmlns:a="http://schemas.datacontract.org/2004/07/System">
<a:DateTime>2013-09-29T22:00:00Z</a:DateTime>
<a:OffsetMinutes>0</a:OffsetMinutes>
</StartsOn>
<WeekAheadForecast>-50</WeekAheadForecast>
<WeekAheadP10>-50</WeekAheadP10>
<WeekAheadP90>-50</WeekAheadP90>
</SolarForecastingChartDataForZoneItem>
<SolarForecastingChartDataForZoneItem>
<DayAheadForecast>-50</DayAheadForecast>
<DayAheadP10>-50</DayAheadP10>
<DayAheadP90>-50</DayAheadP90>
<Forecast>0</Forecast>
<ForecastP10>0</ForecastP10>
<ForecastP90>0</ForecastP90>
<ForecastUpdated>0</ForecastUpdated>
....

恢复等级<Forecast><a:DateTime>

我尝试使用 beautiful soup 和 minidom,例如:

from xml.dom import minidom
xmldoc = minidom.parse('xmlfile')
itemlist = xmldoc.getElementsByTagName('Forecast')
print(len(itemlist)) #to get the number of savings
for s in xmldoc.getElementsByTagName('Forecast'):
print s.nodeValue

但我没有任何值(value)。我想我错了,但我不明白为什么。有人可以帮助我吗?谢谢

最佳答案

不太确定您想要的输出是什么,但当我看到这个问题时,我正在使用 LXML 和 XPATH。

from lxml import html
mystring = ''' I cut and pasted your string here '''
tree = html.fromstring(mystring)
>>> for forecast in tree.xpath('//forecast'):
forecast.text_content()

'0'
'0'
>>> for dtime in tree.xpath('//datetime'):
dtime.text_content()


'2013-09-29T22:00:00Z'
>>>

然后再乱搞一下

all_elements = [e for e in tree.iter()]
for each_element in all_elements[1:]: # The first element is the root - it has all the text without the tags though so I don't want to look at this one
each_element.tag, each_element.text_content()

('errormessage', '')
('intervalinminutes', '15')
('solarforecastingchartdataforzoneitems', '\n \n -50\n -50\n -50\n 0\n 0\n 0\n 0\n -50\n -50\n 0\n 0\n \n 2013-09-29T22:00:00Z\n 0\n \n -50\n -50\n -50\n \n \n -50\n -50\n -50\n 0\n 0\n 0\n 0')
('solarforecastingchartdataforzoneitem', '\n -50\n -50\n -50\n 0\n 0\n 0\n 0\n -50\n -50\n 0\n 0\n \n 2013-09-29T22:00:00Z\n 0\n \n -50\n -50\n -50\n ')
('dayaheadforecast', '-50')
('dayaheadp10', '-50')
('dayaheadp90', '-50')
('forecast', '0')
('forecastp10', '0')
('forecastp90', '0')
('forecastupdated', '0')
('intradayp10', '-50')
.
.
.

关于python - python中的XML解析(Elia结构),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42724863/

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