我有一个代表产品交易的 XML 文件。我想将此数据转换为 pandas 数据框,但不知道如何迭代以获取所有产品级别数据。
这是一个 XML 示例:
<?xml version="1.0" encoding="iso-8859-1"?>
<order-root>
<order ordernumber="12345">
<orderheader>
<customerid>114597</customerid>
<paymentmethod>333</paymentmethod>
<shippingmethod>127</shippingmethod>
<orderdatum>2020-01-01 00:47:42</orderdatum>
<orderstatus>6</orderstatus>
<currency>Dollar</currency>
</orderheader>
<orderrows>
<orderrow orderrownumber="1">
<articlenumber>10004171</articlenumber>
<articlename>Random name 1</articlename>
<unit>1</unit>
<price>2543</price>
<vat>25</vat>
</orderrow>
</orderrows>
</order>
<order ordernumber="23456">
<orderheader>
<customerid>114602</customerid>
<paymentmethod>333</paymentmethod>
<shippingmethod>109</shippingmethod>
<orderdatum>2020-01-01 10:45:45</orderdatum>
<orderstatus>6</orderstatus>
<currency>Dollar</currency>
</orderheader>
<orderrows>
<orderrow orderrownumber="1">
<articlenumber>10448306</articlenumber>
<articlename>Random name 2</articlename>
<unit>1</unit>
<price>235</price>
<vat>25</vat>
</orderrow>
<orderrow orderrownumber="2">
<articlenumber>10448272</articlenumber>
<articlename>Random name 3</articlename>
<unit>1</unit>
<price>109</price>
<vat>25</vat>
</orderrow>
</orderrows>
</order>
<order ordernumber="34567">
<orderheader>
<customerid>114327</customerid>
<paymentmethod>333</paymentmethod>
<shippingmethod>109</shippingmethod>
<orderdatum>2020-01-01 10:45:45</orderdatum>
<orderstatus>6</orderstatus>
<currency>Dollar</currency>
</orderheader>
<orderrows>
<orderrow orderrownumber="1">
<articlenumber>10448306</articlenumber>
<articlename>Random name 2</articlename>
<unit>1</unit>
<price>235</price>
<vat>25</vat>
</orderrow>
<orderrow orderrownumber="2">
<articlenumber>10448272</articlenumber>
<articlename>Random name 3</articlename>
<unit>1</unit>
<price>109</price>
<vat>25</vat>
</orderrow>
<orderrow orderrownumber="3">
<articlenumber>10448562</articlenumber>
<articlename>Random name 4</articlename>
<unit>1</unit>
<price>23</price>
<vat>25</vat>
</orderrow>
</orderrows>
</order>
</order-root>
这就是我期望的最终结果:
订单号 | 客户ID | 付款方式 | 运送方式 | 订单数据 | 订单状态 | 货币 | 订单行号 | 商品编号 | 文章名称 | 单位 | 价格 | 大桶 |
---|---|---|---|---|---|---|---|---|---|---|---|---|
12345 | 114597 | 333 | 127 | 2020-01-01 0:47:42 | 6 | 美元 | 1 | 10004171 | 随机名称1 | 1 | 2543 | 25 |
23456 | 114602 | 333 | 109 | 2020-01-01 10:45:45 | 6 | 美元 | 1 | 10448306 | 随机名称2 | 1 | 235 | 25 |
23456 | 114602 | 333 | 109 | 2020-01-02 10:45:45 | 6 | 美元 | 2 | 10448272 | 随机名称3 | 1 | 109 | 25 |
34567 | 114327 | 333 | 109 | 2020-01-01 10:45:45 | 6 | 美元 | 1 | 10448306 | 随机名称2 | 1 | 235 | 25 |
34567 | 114327 | 333 | 109 | 2020-01-02 10:45:45 | 6 | 美元 | 2 | 10448272 | 随机名称3 | 1 | 109 | 25 |
34567 | 114327 | 333 | 109 | 2020-01-03 10:45:45 | 6 | 美元 | 3 | 10448562 | 随机名称4 | 1 | 23 | 25 |
关于如何迭代数据的问题,可以通过XPath,使用“*”来选择WebElement的所有子元素: 导入操作系统 导入 xml.etree.cElementTree 作为 ET
html_Input = open("path of html", "r")
tree = ET.parse(html_Input)
root = tree.getroot()
for order in root.findall("/order-root/order"):
for child in order.findall("./orderheader/*"):
print(f"{child}\n")
for child in order.findall("./orderrows/*"):
print(f"{child}\n")
html_Input.close()
提到从 XML 创建 pandas 数据框,pandas 已经有了 read_xml
的选项import pandas as pd
df = pd.read_xml("Your XML file", xpath=".//order") #Each order WebElement contains all data for each row.
df
这就是我解决问题所需要的。
将 pandas 导入为 pd df = pd.read_xml("Your XML file", xpath=".//order") #每个订单 WebElement 包含每行的所有数据。 df