我有以下xml数据。你知道吗
<transaction>
<date>20190415</date>
<ticket>303434037</ticket>
<value>15</value>
<notenders>01</notenders>
<tenderdetail>
<tendertype>00</tendertype>
<tenderamt>15</tenderamt>
</tenderdetail>
<item>
<receipeno>00001096</receipeno>
<price>7</price>
<qty>0001</qty>
<items>
<item>
<receipeno>00000786</receipeno>
<price>8</price>
<qty>0001</qty>
<items>
<item>
<receipeno>00000599</receipeno>
<price>0</price>
<qty>1</qty>
</item>
<item>
<receipeno>00000605</receipeno>
<price>0</price>
<qty>1</qty>
</item>
<item>
<receipeno>00000608</receipeno>
<price>0</price>
<qty>0004</qty>
</item>
</items>
</item>
<item>
<receipeno>10000043</receipeno>
<price>0</price>
<qty>0001</qty>
</item>
<item>
<receipeno>00000381</receipeno>
<price>7</price>
<qty>0001</qty>
<items>
<item>
<receipeno>00000607</receipeno>
<price>0</price>
<qty>1</qty>
</item>
</items>
</item>
</items>
</item>
</transaction>
我需要把它转换成表格格式。问题是每个标记中都有许多嵌套的分支。例如许多<item>
&;<items>
标记。不考虑嵌套性。我需要把数据一个一个地列出来。你知道吗
我期望的输出如下
+----------+--------+-------+-----------+------------+-----------+-----------+-------+-----+
| date | ticket | value | notenders | tendertype | tenderamt | receipeno | price | qty |
+----------+--------+-------+-----------+------------+-----------+-----------+-------+-----+
| 20190101 | 12345 | 15 | 1 | 0 | 15 | 1096 | 7 | 1 |
| 20190101 | 12345 | 15 | 1 | 0 | 15 | 786 | 8 | 1 |
| 20190101 | 12345 | 15 | 1 | 0 | 15 | 599 | 0 | 1 |
| 20190101 | 12345 | 15 | 1 | 0 | 15 | 605 | 0 | 1 |
| 20190101 | 12345 | 15 | 1 | 0 | 15 | 608 | 0 | 4 |
| 20190101 | 12345 | 15 | 1 | 0 | 15 | 143 | 0 | 1 |
| 20190101 | 12345 | 15 | 1 | 0 | 15 | 381 | 7 | 1 |
| 20190101 | 12345 | 15 | 1 | 0 | 15 | 607 | 0 | 1 |
+----------+--------+-------+-----------+------------+-----------+-----------+-------+-----+
我不熟悉python和XML解析。因此,请指示我解决这个问题。 ... 你知道吗
您可以尝试使用以下代码从嵌套的XML文件中获取所有数据,但我认为有一种优雅的方法可以实现此结果:
输出:
希望这能对你有所帮助。你知道吗
从必要的导入开始:
然后,要从要读取的标记中删除前导零,请定义以下函数:
要读取源文件及其根元素,请执行:
要从根级别读取标记(而不是从项读取),请执行:
剩下的两个标记向下一层,所以从读取它们的父标记开始:
从中读出这些标签:
请注意,我在这里使用了stripLZ函数(将使用它) 几倍以上)。你知道吗
现在有时间创建结果数据帧:
加载回路可采用iter法进行:
此循环:
相关问题 更多 >
编程相关推荐