Link to home
Start Free TrialLog in
Avatar of Scott Abraham
Scott AbrahamFlag for United States of America

asked on

Python ElementTree xml output to csv with conditions

I have the following script:

import xml.etree.ElementTree as ET
import csv

tree = ET.parse('registerreads.xml')
root = tree.getroot()[3]

with open('registerreads.csv', 'wb') as fout:
    writer = csv.writer(fout)
    row = ['Value', 'ReadingTime', 'EntityId']
    writer.writerow(row)
    for channel in tree.iter('Channel'):
        for exportrequest in channel.iter('ExportRequest'):
            entityid = exportrequest.attrib.get('EntityID')
            for meterread in channel.iter('Reading'):
                read = meterread.attrib.get('Value')
                date = meterread.attrib.get('ReadingTime')
                row = [read[:-2], date[:10], entityid]
                print ', '.join(row)
                writer.writerow(row)

which parses and outputs from this part of the tree in the xml file:

<Channel StartDate="2016-02-14T00:00:00-06:00" EndDate="2016-02-15T00:00:00-06:00" TimeZone="CentralUS" IsRegister="true" MarketType="Electric" IntervalLength="-1" NumberOfDials="5" PulseMultiplier="-1" PressureCompensationFactor="1" IsReadingDecoded="true" ReadingsInPulse="false">
      <ChannelID ServicePointChannelID="74325661:150" />
      <Readings>
        <Reading Value="8.4" ReadingTime="2016-02-14T17:05:00-06:00" StatusRef="1" />
      </Readings>
      <ExportRequest RequestID="270" EntityType="ServicePoint" EntityID="74325661" RequestSource="Scheduled" />
    </Channel>
    <Channel StartDate="2016-02-14T00:00:00-06:00" EndDate="2016-02-15T00:00:00-06:00" TimeZone="CentralUS" IsRegister="true" MarketType="Electric" IntervalLength="-1" NumberOfDials="5" PulseMultiplier="-1" PressureCompensationFactor="1" IsReadingDecoded="true" ReadingsInPulse="false">
      <ChannelID ServicePointChannelID="74325661:301" />
      <Readings>
        <Reading Value="173.2" ReadingTime="2016-02-14T00:00:00-06:00" StatusRef="1" />
        <Reading Value="209.1" ReadingTime="2016-02-15T00:00:00-06:00" StatusRef="1" />
      </Readings>
      <ExportRequest RequestID="270" EntityType="ServicePoint" EntityID="74325661" RequestSource="Scheduled" />
    </Channel>

for these results:
Value,ReadingTime,EntityId,ServicePointChannelID
8,2016-02-14,74325661
173,2016-02-14,74325661
209,2016-02-15,74325661

I want the script to grab only the data where <ChannelID ServicePointChannelID="74325661:301" /> is the :301.  The First data row in the output file is actually from a :150, no a :301.

Any help is appreciated.
ASKER CERTIFIED SOLUTION
Avatar of Brendt Hess
Brendt Hess
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Scott Abraham

ASKER

That is it!  Thank you!