我正在尝试使用python中的元素树来解析XML文件。我已附上快照XML data。我需要提取TimeSeries标记下的所有内容并将其导出为CSV。
我已将文件保存到计算机上,因此代码中的名称为save.xml。我试图以mRID和CurveType为例,但这对我没有用。这是我尝试的代码。
import xml.etree.cElementTree as ET
tree = ET.parse('save.xml')
root = tree.getroot()
for TimeSeries in root.findall('TimeSeries'):
mRID = TimeSeries.find('mRID').text
curve = TimeSeries.get ('curveType')
我将如何获取位于timeseries标签下的所有内容并以CSV格式导出?
-编辑具有相同问题的任何人-
代码现在变成了这样,因为我们需要在标签的前面添加名称空间(或者,如果方便的话就删除它):
#fix namespace issue
ns = {'s': 'urn:iec62325.351:tc57wg16:451-6:generationloaddocument:3:0'}
# use s and namespace in front of all findall
for TimeSeries in root.findall('s:TimeSeries', ns):
mRID = TimeSeries.find('s:mRID', ns)
businessType = TimeSeries.find('s:businessType', ns)
objectAggregation = TimeSeries.find('s:objectAggregation', ns)
unit = TimeSeries.find('s:quantity_Measure_Unit.name', ns)
curveType = TimeSeries.find('s:curveType', ns)
答案 0 :(得分:-1)
#Something like this can be done to fetch the data from xml file`enter code here`
import xml.etree.ElementTree as etree
tree = etree.patse('save.xml')
root = tree.getroot()
for timeseries in root.iter():
print timeseries.get('mRID')
print timeseries.get('curveType')