2 回答

TA贡献1848条经验 获得超6个赞
这是我使用熊猫从自己那里尝试的,我能够正确获得输出。
import pandas as pd
import xml.etree.ElementTree as etree
tree = etree.parse("Filename.xml") #enter your filename what you saved in your system
root = tree.getroot()
columns = ["name", "type"]
datatframe = pd.DataFrame(columns = columns)
for node in root[0][0][2][0]:
name = node.get("name")
type = node.get("type")
datatframe = datatframe.append(pd.Series([name, type], index = columns), ignore_index = True)
print(datatframe)
我的输出:
name type
0 hospital_no string
1 partner_id integer
2 district string
3 town string
4 date string
5 cat1_uom_count integer
6 cat1_uom_qty float
7 cat2_uom_count integer
8 cat2_uom_qty float
9 cat3_uom_count integer
10 cat3_uom_qty float
11 cat4_uom_count integer
12 cat4_uom_qty float
13 cat5_uom_count integer
14 cat5_uom_qty float
15 total_uom_count integer
16 total_uom_qty float
17 plant_id integer
18 vehicle_id integer

TA贡献1789条经验 获得超8个赞
您并没有真正说明要实现的目标,但下面是从xml中提取数据的示例,在本例中,我从树元素内的字段中提取字段名称和类型。
import xmltodict
with open("test.xml") as xml_file:
my_xml = xmltodict.parse(xml_file.read())
for field in my_xml["openerp"]["data"]["record"]["field"][2]["tree"]["field"]:
print(f"{field['@name']}: {field['@type']}")
输出
hospital_no: string
partner_id: integer
district: string
town: string
date: string
cat1_uom_count: integer
cat1_uom_qty: float
cat2_uom_count: integer
cat2_uom_qty: float
cat3_uom_count: integer
cat3_uom_qty: float
cat4_uom_count: integer
cat4_uom_qty: float
cat5_uom_count: integer
cat5_uom_qty: float
total_uom_count: integer
total_uom_qty: float
plant_id: integer
vehicle_id: integer
添加回答
举报