我是python的初学者。即使有很多xml到csv的示例,但是当我做某事时,我仍然没有得到这个关键点。 xml文件如下:
<?xml version="1.0" encoding="ISO-8859-1"?>
<spec:BACKUPCFG xmlns:spec="http://" >
<spec:fileHeader fileFormatVersion="1.2.0" nenrmversion="BTS3900DATAV100R012C10SPC100" neversion="BTS3900 V100R012C10SPC100" syntype="synall" synlabel="00000519320218576688" producttype="117"/>
<spec:compatibleNrmVersionList>
<spec:compatibleNrmVersion>B100R012C10SPC100</spec:compatibleNrmVersion>
</spec:compatibleNrmVersionList>
<spec:syndata FunctionType="NODE" Id="NODENAME=Node" productversion="BTS3900 V100R012C10SPC100" nermversion="HERTBBUV500R007C10SPC100" objId="-1">
<spec:compatibleNrmVersionList>
<spec:compatibleNrmVersion>HERTBBUV500R007</spec:compatibleNrmVersion>
</spec:compatibleNrmVersionList>
<class>
<Cell>
<attributes>
<LocalCellId>60</LocalCellId>
<CellName>NJ0003P_9NB01_S01</CellName>
<CsgInd>0</CsgInd><!--False-->
<UlCyclicPrefix>0</UlCyclicPrefix><!--Normal-->
<DlCyclicPrefix>0</DlCyclicPrefix><!--Normal-->
<FreqBand>8</FreqBand>
<UlEarfcnCfgInd>0</UlEarfcnCfgInd><!--Not configure-->
<DlEarfcn>3750</DlEarfcn>
<UlBandWidth>2</UlBandWidth><!--5M-->
<DlBandWidth>2</DlBandWidth><!--5M-->
<CellId>60</CellId>
<PhyCellId>128</PhyCellId>
</attributes>
</Cell>
<Cell>
<attributes>
<LocalCellId>61</LocalCellId>
<CellName>NJ0003P_9NB01_S02</CellName>
<CsgInd>0</CsgInd><!--False-->
<UlCyclicPrefix>0</UlCyclicPrefix><!--Normal-->
<DlCyclicPrefix>0</DlCyclicPrefix><!--Normal-->
<FreqBand>8</FreqBand>
<UlEarfcnCfgInd>0</UlEarfcnCfgInd><!--Not configure-->
<DlEarfcn>3750</DlEarfcn>
<UlBandWidth>2</UlBandWidth><!--5M-->
<DlBandWidth>2</DlBandWidth><!--5M-->
<CellId>61</CellId>
<PhyCellId>192</PhyCellId>
</attributes>
</Cell>
<Cell>
<attributes>
<LocalCellId>62</LocalCellId>
<CellName>CBR0003P_9NB01_S03</CellName>
<CsgInd>0</CsgInd><!--False-->
<UlCyclicPrefix>0</UlCyclicPrefix><!--Normal-->
<DlCyclicPrefix>0</DlCyclicPrefix><!--Normal-->
<FreqBand>8</FreqBand>
<UlEarfcnCfgInd>0</UlEarfcnCfgInd><!--Not configure-->
<DlEarfcn>3750</DlEarfcn>
<UlBandWidth>2</UlBandWidth><!--5M-->
<DlBandWidth>2</DlBandWidth><!--5M-->
<CellId>62</CellId>
<PhyCellId>194</PhyCellId>
</attributes>
</Cell>
<Cell>
<attributes>
<LocalCellId>63</LocalCellId>
<CellName>NJ0003P_9NB01_S04</CellName>
<CsgInd>0</CsgInd><!--False-->
<UlCyclicPrefix>0</UlCyclicPrefix><!--Normal-->
<DlCyclicPrefix>0</DlCyclicPrefix><!--Normal-->
<FreqBand>8</FreqBand>
<UlEarfcnCfgInd>0</UlEarfcnCfgInd><!--Not configure-->
<DlEarfcn>3750</DlEarfcn>
<UlBandWidth>2</UlBandWidth><!--5M-->
<DlBandWidth>2</DlBandWidth><!--5M-->
<CellId>63</CellId>
<PhyCellId>424</PhyCellId>
</attributes>
</Cell>
</class>
<class>
<CellDrxSpecialPara>
<attributes>
<LocalCellId>60</LocalCellId>
<CellDrxSpecialParaValid>0</CellDrxSpecialParaValid><!--FALSE-->
<LongDrxCycleSpecial>0</LongDrxCycleSpecial><!--10 subframes-->
<OnDurationTimerSpecial>4</OnDurationTimerSpecial><!--5 PDCCH subframes-->
</attributes>
</CellDrxSpecialPara>
<CellDrxSpecialPara>
<attributes>
<LocalCellId>61</LocalCellId>
<CellDrxSpecialParaValid>0</CellDrxSpecialParaValid><!--FALSE-->
<LongDrxCycleSpecial>0</LongDrxCycleSpecial><!--10 subframes-->
<OnDurationTimerSpecial>4</OnDurationTimerSpecial><!--5 PDCCH subframes-->
</attributes>
</CellDrxSpecialPara>
<CellDrxSpecialPara>
<attributes>
<LocalCellId>64</LocalCellId>
<CellDrxSpecialParaValid>0</CellDrxSpecialParaValid><!--FALSE-->
<LongDrxCycleSpecial>0</LongDrxCycleSpecial><!--10 subframes-->
<OnDurationTimerSpecial>4</OnDurationTimerSpecial><!--5 PDCCH subframes-->
</attributes>
</CellDrxSpecialPara>
<CellDrxSpecialPara>
<attributes>
<LocalCellId>65</LocalCellId>
<CellDrxSpecialParaValid>0</CellDrxSpecialParaValid><!--FALSE-->
<LongDrxCycleSpecial>0</LongDrxCycleSpecial><!--10 subframes-->
<OnDurationTimerSpecial>4</OnDurationTimerSpecial><!--5 PDCCH subframes-->
</attributes>
</CellDrxSpecialPara>
</class>
</spec:syndata>
<spec:fileFooter label="00000519320218576688" ExportResult="Success" dateTime="2017-05-15T08:21:09">
<spec:moclistincluded></spec:moclistincluded>
</spec:fileFooter>
</spec:BACKUPCFG>
<!--CRC=0x062E67D4BBFE31B20E6886224D77AA640AEE7313098F53CE7B2C831CB4905DA6-->
import xml.etree.ElementTree as ET
import csv
tree = ET.parse('xml-csv/CFGDATA.XML')
root = tree.getroot()
with open('config.csv', 'w', newline = '') as r:
writer = csv.writer(r)
writer.writerow(['LocalCellId','CellName','FreqBand','DlEarfcn','CellId','PhyCellId'])
for mr in tree.iter('Cell'):
#for mr in cell.iter('Cell'):
LocalCellId = mr.attrib.get('LocalCellId')
CellName = mr.attrib.get('CellName')
FreqBand = mr.attrib.get('FreqBand')
DlEarfcn = mr.attrib.get('DlEarfcn')
CellId = mr.attrib.get('CellId')
PhyCellId = mr.attrib.get('PhyCellId')
writer.writerow([LocalCellId,CellName,FreqBand,DlEarfcn,CellId,PhyCellId])
我想从xml中获取CSV数据,如下表所示,但我不能。
答案 0 :(得分:1)
使用lxml:
from lxml import etree
xmlparser = etree.XMLParser()
with open('xml-csv/CFGDATA.XML') as xmlfile:
tree = etree.parse(xmlfile, xmlparser)
cells = tree.xpath("//Cell")
for cell in cells:
LocalCellId = cell.find('attributes/LocalCellId').text
CellName = cell.find('attributes/CellName').text
FreqBand = cell.find('attributes/FreqBand').text
DlEarfcn = cell.find('attributes/DlEarfcn').text
CellId = cell.find('attributes/CellId').text
PhyCellId = cell.find('attributes/PhyCellId').text
print(LocalCellId,CellName,FreqBand,DlEarfcn,CellId,PhyCellId,sep=',')
输出:
60,NJ0003P_9NB01_S01,8,3750,60,128
61,NJ0003P_9NB01_S02,8,3750,61,192
62,CBR0003P_9NB01_S03,8,3750,62,194
63,NJ0003P_9NB01_S04,8,3750,63,424