Scott Abraham
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.xm l')
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('ExportReques t'):
entityid = exportrequest.attrib.get(' EntityID')
for meterread in channel.iter('Reading'):
read = meterread.attrib.get('Valu e')
date = meterread.attrib.get('Read ingTime')
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:0 0: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="743 25661: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:0 0: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="743 25661: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 ,ServicePo intChannel ID
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="743 25661:301" /> is the :301. The First data row in the output file is actually from a :150, no a :301.
Any help is appreciated.
import xml.etree.ElementTree as ET
import csv
tree = ET.parse('registerreads.xm
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('ExportReques
entityid = exportrequest.attrib.get('
for meterread in channel.iter('Reading'):
read = meterread.attrib.get('Valu
date = meterread.attrib.get('Read
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:0
<ChannelID ServicePointChannelID="743
<Readings>
<Reading Value="8.4" ReadingTime="2016-02-14T17
</Readings>
<ExportRequest RequestID="270" EntityType="ServicePoint" EntityID="74325661" RequestSource="Scheduled" />
</Channel>
<Channel StartDate="2016-02-14T00:0
<ChannelID ServicePointChannelID="743
<Readings>
<Reading Value="173.2" ReadingTime="2016-02-14T00
<Reading Value="209.1" ReadingTime="2016-02-15T00
</Readings>
<ExportRequest RequestID="270" EntityType="ServicePoint" EntityID="74325661" RequestSource="Scheduled" />
</Channel>
for these results:
Value,ReadingTime,EntityId
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="743
Any help is appreciated.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER