Im trying to use element tree in python to parse a XML file. I have attached a snap shot XML data. I need to pull everything that is under the TimeSeries Tag and export it as a CSV.
I have saved the file onto my computer hence the name save.xml in the code. I tried to just pull mRID and CurveType as an example and that didnt work for me. This is the code i tried.
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')
How would i grab everything that sits under the timeseries tag and export is as CSV?
--EDIT FOR ANYONE HAVING THE SAME ISSUE--
the code now becomes this because we need to add the namespace (or remove it if that's easier) to the front of the tags:
#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)