【发布时间】:2020-05-05 04:35:23
【问题描述】:
我有如下数据结构(原来是2.5gb,所以必须解析):
<households xmlns="http://www.matsim.org/files/dtd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.matsim.org/files/dtd http://www.matsim.org/files/dtd/households_v1.0.xsd">
<household id="1473">
<members>
<personId refId="2714"/>
<personId refId="2715"/>
<personId refId="2716"/>
<personId refId="2717"/>
<personId refId="2718"/>
<personId refId="2719"/>
</members>
<income currency="CHF" period="month">
3094.87101
</income>
<attributes>
<attribute name="bikeAvailability" class="java.lang.String" >some</attribute>
<attribute name="carAvailability" class="java.lang.String" >some</attribute>
<attribute name="consumptionUnits" class="java.lang.Double" >3.3</attribute>
<attribute name="householdIncomePerConsumptionUnit" class="java.lang.Double" >3094.8710104279835</attribute>
<attribute name="numberOfCars" class="java.lang.Integer" >1</attribute>
<attribute name="residenceZoneCategory" class="java.lang.Integer" >1</attribute>
<attribute name="totalHouseholdIncome" class="java.lang.Double" >10213.074334412346</attribute>
</attributes>
</household>
<household id="2474">
<members>
<personId refId="4647"/>
<personId refId="4648"/>
<personId refId="4649"/>
<personId refId="4650"/>
<personId refId="4651"/>
<personId refId="4652"/>
<personId refId="4653"/>
<personId refId="4654"/>
<personId refId="4655"/>
</members>
<income currency="CHF" period="month">
1602.562822
</income>
<attributes>
<attribute name="bikeAvailability" class="java.lang.String" >none</attribute>
<attribute name="carAvailability" class="java.lang.String" >all</attribute>
<attribute name="consumptionUnits" class="java.lang.Double" >3.6999999999999997</attribute>
<attribute name="householdIncomePerConsumptionUnit" class="java.lang.Double" >1602.5628215679633</attribute>
<attribute name="numberOfCars" class="java.lang.Integer" >1</attribute>
<attribute name="residenceZoneCategory" class="java.lang.Integer" >1</attribute>
<attribute name="totalHouseholdIncome" class="java.lang.Double" >5929.482439801463</attribute>
</attributes>
</household>
<household id="4024">
<members>
<personId refId="7685"/>
</members>
<income currency="CHF" period="month">
61610.096619
</income>
<attributes>
<attribute name="bikeAvailability" class="java.lang.String" >none</attribute>
<attribute name="carAvailability" class="java.lang.String" >none</attribute>
<attribute name="consumptionUnits" class="java.lang.Double" >1.0</attribute>
<attribute name="householdIncomePerConsumptionUnit" class="java.lang.Double" >61610.096618936936</attribute>
<attribute name="numberOfCars" class="java.lang.Integer" >0</attribute>
<attribute name="residenceZoneCategory" class="java.lang.Integer" >1</attribute>
<attribute name="totalHouseholdIncome" class="java.lang.Double" >61610.096618936936</attribute>
</attributes>
</household>
</households>
我想提取所有person ID refId 值及其对应的income 值。最终,我计划有一个包含 personId 列和收入列的 df(收入将是重复的)。所以棘手的部分不仅是命名空间,还有如何在不同的节点级别访问 XML。
到目前为止,我的方法未能做到这一点。
import gzip
import xml.etree.ElementTree as ET
from collections import defaultdict
import pandas as pd
import numpy as np
tree = ET.parse(gzip.open('V0_1pm/output_households.xml.gz', 'r'))
root = tree.getroot()
rows = []
for it in root.iter('household'):
hh = it.attrib['id']
inc = it.find('income').text
rows.append([hh,inc])
hh_inc = pd.DataFrame(rows, columns=['id', 'PTSubscription'])
hh_inc
非常感谢任何帮助。
【问题讨论】:
-
"因此必须解析"你是什么意思?
-
你的方法是如何失败的?
-
我会支持其他 cmets 的要求。这里真的没什么可做的。
-
您的 xml 有一个命名空间 - Parsing XML with Namespaces。
root.iter('{http://www.matsim.org/files/dtd}household') -
@mzjn 我的意思是,如果没有这种方法,我的计算机没有计算能力来提取信息。
标签: python xml pandas parsing elementtree