2 回答

TA貢獻1848條經(jīng)驗 獲得超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條經(jīng)驗 獲得超8個贊
您并沒有真正說明要實現(xiàn)的目標,但下面是從xml中提取數(shù)據(jù)的示例,在本例中,我從樹元素內(nèi)的字段中提取字段名稱和類型。
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
添加回答
舉報