简体   繁体   English

Python ElementTree XML输出到CSV

[英]Python ElementTree xml output to csv

I have the following XML file ('registerreads_EE.xml'): 我有以下XML文件(“ registerreads_EE.xml”):

<?xml version="1.0" encoding="us-ascii" standalone="yes"?>
<ReadingDocument xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ReadingStatusRefTable>
<ReadingStatusRef Ref="1">
  <UnencodedStatus SourceValidation="SIMPLE">
    <StatusCodes>
      <Signal>XX</Signal>
    </StatusCodes>
  </UnencodedStatus>
</ReadingStatusRef>
  </ReadingStatusRefTable>
  <Header>
<IEE_System Id="XXXXXXXXXXXXXXX" />
<Creation_Datetime Datetime="2015-10-22T09:05:32Z" />
<Timezone Id="UTC" />
<Path FilePath="X:\XXXXXXXXXXXX.xml" />
<Export_Template Id="XXXXX" />
<CorrelationID Id="" />
  </Header>
  <ImportExportParameters ResubmitFile="false" CreateGroup="true">
    <DataFormat TimestampType="XXXXXX" Type="XXXX" />
  </ImportExportParameters>
  <Channels>
<Channel StartDate="2015-10-21T00:00:00-05:00" EndDate="2015-10-22T00:00:00-05:00">
  <ChannelID ServicePointChannelID="73825603:301" />
  <Readings>
    <Reading Value="3577.0" ReadingTime="2015-10-21T00:00:00-05:00" StatusRef="1" />
    <Reading Value="3601.3" ReadingTime="2015-10-22T00:00:00-05:00" StatusRef="1" />
  </Readings>
  <ExportRequest RequestID="152" EntityType="ServicePoint" EntityID="73825603" RequestSource="Scheduled" />
</Channel>
    <Channel StartDate="2015-10-21T00:00:00-05:00" EndDate="2015-10-22T00:00:00-05:00">
  <ChannelID ServicePointChannelID="73825604:301" />
  <Readings>
    <Reading Value="3462.5" ReadingTime="2015-10-21T00:00:00-05:00" StatusRef="1" />
    <Reading Value="3501.5" ReadingTime="2015-10-22T00:00:00-05:00" StatusRef="1" />
  </Readings>
  <ExportRequest RequestID="152" EntityType="ServicePoint" EntityID="73825604" RequestSource="Scheduled" />
</Channel>
  </Channels>
</ReadingDocument>

I want to parse the XML of the channel data into a csv file. 我想将通道数据的XML解析为一个csv文件。

He is what I have written in Python 2.7.10: 他是我用Python 2.7.10编写的:

import xml.etree.ElementTree as ET

tree = ET.parse('registerreads_EE.xml')

root = tree.getroot()[3]

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')
            print read[:-2],",",date[:10],",",entityid

tree.write(open('registerreads_EE.csv','w'))

Here is the screen output when the above is run: 这是运行上述命令时的屏幕输出:

3577 , 2015-10-21 , 73825603
3601 , 2015-10-22 , 73825603
3462 , 2015-10-21 , 73825604
3501 , 2015-10-22 , 73825604

The 'registerreads.csv' output file is like the original XML file, minus the first line. “ registerreads.csv”输出文件类似于原始XML文件,但减去第一行。

I would like the printed output above outputted to a csv file with headers of read, date, entityid. 我想将上面的打印输出输出到带有读取,日期,entityid标头的csv文件。

I am having difficulty with this. 我对此有困难。 This is my first python program. 这是我的第一个python程序。 Any help is appreciated. 任何帮助表示赞赏。

Use the csv module not lxml module to write rows to csv file. 使用csv模块而非lxml模块将行写入csv文件。 But still use lxml to parse and extract content from xml file: 但是仍然使用lxml解析和提取xml文件中的内容:

import xml.etree.ElementTree as ET
import csv

tree = ET.parse('registerreads_EE.xml')

root = tree.getroot()[3]

with open('registerreads_EE.csv', 'w', newline='') as r:
    writer = csv.writer(r)
    writer.writerow(['read', 'date', 'entityid'])  # WRITING HEADERS

    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')    

                # WRITE EACH ROW ITERATIVELY 
                writer.writerow([read[:-2],date[:10],entityid])  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM