我正在尝试解析以下XML文件并将其输出到CSV文件,如下所示。我尝试过elementtree。但是,它不打印任何输出。
trial.xml
<?xml version="1.0">
<!-- random text -->
<!-- random text -->
<meta>
<valID>
<ValueID>age</ValueID>
<desc>Physical attributes | Age at time of evaluation</desc>
<group>
<ord>1</ord>
<name>0 - <5</name>
<min>0</min>
<mininc>TRUE</mininc>
<max>5</max>
<maxinc>FALSE</maxinc>
</group>
<group>
<ord>2</ord>
<name>5 - <10</name>
<min>5</min>
<mininc>TRUE</mininc>
<max>10</max>
<maxinc>FALSE</maxinc>
</group>
</valID>
</meta>
所需的输出(output.csv)
ValueID,name,min,maxinc
age,1,0-<5,0,5
age,5-<10,5,10
我试过的代码:
from xml.etree import ElementTree
document = ElementTree.parse( 'trial.xml' )
for d in document.findall('meta/valID/ValueID'):
print d.text