1
votes

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)
1
That URL is not accessible. Additionally, you should explicitly ask a question here. - drootang
I have attached an snap shot of the data in image form. I would like to know how i can get all the data which sits under TimeSeries and export is as CSV. - KateNik
An image isn’t very helpful because it doesn’t allow anyone to easily test/reproduce your issue. However looking at the screenshot I see your xml has a default namespace (the xmlns) try searching on ElementTree default namespace; there are tons of examples. If you still have problems, update your question and we’ll be happy to help. - Daniel Haley

1 Answers

-1
votes
#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')