【发布时间】:2018-04-08 16:01:00
【问题描述】:
迫切需要帮助。我是 Python 的初学者,并且尝试了几天(和几夜)来做到这一点,但没有成功。拥有大型 xml 文件,其中包含具有可变子子元素(即属性值)的子元素(即属性)的元素(即帐户)。由于子子元素是可变的,我不知道如何让它深入到它需要拾取所有内容并将其放入.csv 中。 因此,每个帐户可能有很多记录。我想要一行包含帐户 ID,后跟属性名称,然后是属性值。如果一个帐户有很多属性,他们可以有很多行。
非常感谢您提供的任何帮助! :)
<?xml version="1.0" encoding="UTF-8"?>
<rbacx>
<namespace namespaceName="ABC RSS : xxxxxxx" namespaceShortName="RSS" />
<attributeValues />
<accounts>
<account id="AAGALY2">
<name>AAGALY2</name>
<endPoint>ABCD</endPoint>
<domain>ABCD</domain>
<comments />
<attributes> ### one account can have many attribute records
<attribute name="appUserName">
<attributeValues>
<attributeValue>
<value><![CDATA[A, Agglya]]></value>
</attributeValue>
</attributeValues>
</attribute>
<attribute name="costCentre">
<attributeValues>
<attributeValue>
<value><![CDATA[6734]]></value>
</attributeValue>
</attributeValues>
</attribute>
<attribute name="App ID">
<attributeValues>
<attributeValue>
<value><![CDATA[AAGALY2]]></value>
</attributeValue>
</attributeValues>
</attribute>
<attribute name="Last Access Date">
<attributeValues>
<attributeValue>
<value><![CDATA[00000000]]></value>
etc......
希望 csv 看起来像这样:
AcctName Endpoint Domain AttribName AttribValue
AAGALY2 ABCD ABCD appUserName A, Agalya
AAGALY2 ABCD ABCD CostCentre 333333
AAGALY2 ABCD ABCD App ID AAGALY2
AAGALY2 ABCD ABCD Jobtemplate A12-can read
JSMITH1 EFG ABCD appUserName J, Smith
JSMITH1 ABCD ABCD CostCentre 12345
JSMITH1 ABCD ABCD Jobtemplate A22-perm to write
ZZMITH3 EFG GHI appUserName Z, Zmith
ZZMITH3 EFG GHI CostCentre 3456
【问题讨论】:
-
xml.etree.ElementTree。它有据可查,并且有很多例子。祝你好运
标签: python xml-parsing