如何在python中将xml文件转换为csv
我是python的初学者。即使有很多xml到csv的例子,但当我做一些事情时,我仍然不明白这个关键点。xml文件如下所示:如何在python中将xml文件转换为csv,python,Python,我是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"
<?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-->
我想从xml中获取csv数据,就像下表一样,但我做不到
使用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
您是否得到错误,或者您的输出与您预期的不同?请描述一下你现在得到了什么。发生了什么?你有输出吗?对吗?如果没有,这是怎么回事?抱歉,运行此代码时遇到如下错误:--------------------------------------------------------------------------------------PermissionError回溯(最近一次调用)在()1 root=tree.getroot()2-->3中打开('config.csv','w',newline='')as r:4 writer=csv.writer(r)5 writer.writerow(['LocalCellId','CellName','frequeband','DlEarfcn','CellId','PhyCellId'])权限错误:[Errno 13]权限被拒绝:'config.csv':-)如果答案有效,请接受它(在投票分数下打勾)。非常感谢。
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