【问题标题】:Parse xml file to data frame in python在python中将xml文件解析为数据框
【发布时间】:2020-06-29 10:11:16
【问题描述】:

我希望你能帮助我。

我想从这种 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

非常感谢您的帮助。

【问题讨论】:

    标签: python xml parsing


    【解决方案1】:

    下面:

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-05
      • 1970-01-01
      • 2011-07-28
      相关资源
      最近更新 更多