python - 在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
非常感谢你的帮助。
解决方案
以下:
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
推荐阅读
- polarion - 是否可以在更改请求 WI 被批准之前限制 SVN 中的提交
- java - 在 RStudio 中安装/导入 XLConnect 和 rJava 时出现问题
- macos - SwiftUI:如何从 macOS 上的联系人中拖放联系人
- android - DrawableStart 惹恼了我
- java - 自定义 AOP spring boot 注释,带有功能区客户端阻止返回“1”的 api 调用
- excel - 循环删除重复项仅适用于第一次迭代
- java - How to login two user types (like admin and user) using single login activity in SQLite Android?
- c# - App opens console window when being build with Docker
- r - Strange behaviour when ggplotting line thickness depending on field of a sf "MULTILINESTRING"
- node.js - Gupshup whatsApp api 在收到 webhook 后返回 200 时发送“ok”消息