首页 > 解决方案 > 在python中将xml文件解析为数据框

问题描述

我希望你能帮助我。

我想从此 XML 文件格式中获取数据。

<person id="10000115">
    <attributes>
        <attribute name="age" class="java.lang.Integer" >25</attribute>
        <attribute name="carAvailability" class="java.lang.String" >never</attribute>
        <attribute name="censusId" class="java.lang.Integer" >449528</attribute>
        <attribute name="employment" class="java.lang.String" >yes</attribute>
        <attribute name="htsId" class="java.lang.String" >1009431</attribute>
        <attribute name="sex" class="java.lang.String" >m</attribute>
    </attributes>
    <attributes>
            <attribute name="age" class="java.lang.Integer" >55</attribute>
            <attribute name="carAvailability" class="java.lang.String" >never</attribute>
            <attribute name="censusId" class="java.lang.Integer" >450886</attribute>
            <attribute name="employment" class="java.lang.String" >yes</attribute>
            <attribute name="htsId" class="java.lang.String" >1023573</attribute>
            <attribute name="sex" class="java.lang.String" >m</attribute>
        </attributes>
<person>

我试过使用这段代码

for person in root.iter('person'):
    #root1 = ET.Element('root')
    #print(person.attrib)
    root1 = person
    for attribute in root1.iter('attribute'):
        Id.append(person.attrib['id'])
        #Age = attribute.find('attribute').text
        Age = attribute.attrib.get('attributes')

但我没有得到结果。

我期望的结果是这样的

id, age, carAvailibility, censusId, employment, htsId, sex
10000115, 25, never, 449528, yes,1009431,m
10000200, 55, never, 450886, yes, 1023573, m

非常感谢你的帮助。

标签: pythonxmlparsing

解决方案


以下:

import xml.etree.ElementTree as ET
import pandas as pd

xml = '''<person id="10000115">
    <attributes>
        <attribute name="age" class="java.lang.Integer" >25</attribute>
        <attribute name="carAvailability" class="java.lang.String" >never</attribute>
        <attribute name="censusId" class="java.lang.Integer" >449528</attribute>
        <attribute name="employment" class="java.lang.String" >yes</attribute>
        <attribute name="htsId" class="java.lang.String" >1009431</attribute>
        <attribute name="sex" class="java.lang.String" >m</attribute>
    </attributes>
    <attributes>
            <attribute name="age" class="java.lang.Integer" >55</attribute>
            <attribute name="carAvailability" class="java.lang.String" >never</attribute>
            <attribute name="censusId" class="java.lang.Integer" >450886</attribute>
            <attribute name="employment" class="java.lang.String" >yes</attribute>
            <attribute name="htsId" class="java.lang.String" >1023573</attribute>
            <attribute name="sex" class="java.lang.String" >m</attribute>
        </attributes>
</person>'''

data = []

root = ET.fromstring(xml)
attrs = root.findall('.//attributes')
for attr in attrs:
    temp = {}
    for entry in list(attr):
        temp[entry.attrib['name']] = entry.text
    data.append(temp)
print(data)
df = pd.DataFrame(data)
print(df)

输出:

[{'age': '25', 'carAvailability': 'never', 'censusId': '449528', 'employment': 'yes', 'htsId': '1009431', 'sex': 'm'},
 {'age': '55', 'carAvailability': 'never', 'censusId': '450886', 'employment': 'yes', 'htsId': '1023573', 'sex': 'm'}]
  age carAvailability censusId employment    htsId sex
0  25           never   449528        yes  1009431   m
1  55           never   450886        yes  1023573   m

推荐阅读