У меня есть этот XML файл:
<population>
<person id="101">
<attributes>
<attribute name="age" class="java.lang.Integer" >53</attribute>
</attributes>
<plan score="-0.38" selected="yes">
<activity type="outside" link="81312" facility="outside_208" x="649324.9906891582" y="6866581.699995641" end_time="08:22:00" >
</activity>
<leg mode="car" dep_time="08:22:00" trav_time="00:10:13">
<route type="links" start_link="81312" end_link="138852" trav_time="00:10:13" distance="6046.54932060571" vehicleRefId="7262234">81312</route>
</leg>
<activity type="work" link="138852" facility="38407" x="651680.6" y="6863892.5" start_time="08:45:22" end_time="17:15:22" >
<attributes>
<attribute name="innerParis" class="java.lang.Boolean" >true</attribute>
</attributes>
</activity>
<leg mode="car" dep_time="17:15:22" trav_time="00:07:05">
<route type="links" start_link="138852" end_link="189898" trav_time="00:07:05" distance="4604.544053407517" vehicleRefId="7262234">138852</route>
</leg>
<activity type="outside" link="189898" facility="outside_249" x="648729.9598002436" y="6866057.250182923" end_time="17:20:35" >
</activity>
</plan>
<plan score="-0.38" selected="no">
<activity type="inside" link="81312" facility="outside_208" x="649324.9906891582" y="6866581.699995641" end_time="08:22:00" >
</activity>
<leg mode="bike" dep_time="08:22:00" trav_time="00:10:13">
<route type="links" start_link="81312" end_link="138852" trav_time="00:10:13" distance="6046.54932060571" vehicleRefId="7262234">81312</route>
</leg>
<activity type="shopping" link="138852" facility="38407" x="651680.6" y="6863892.5" start_time="08:45:22" end_time="17:15:22" >
<attributes>
<attribute name="innerParis" class="java.lang.Boolean" >true</attribute>
</attributes>
</activity>
<leg mode="bike" dep_time="08:22:00" trav_time="00:10:13">
<route type="links" start_link="81312" end_link="138852" trav_time="00:10:13" distance="6046.54932060571" vehicleRefId="7262234">81312</route>
</leg>
<activity type="work" link="138852" facility="38407" x="651680.6" y="6863892.5" start_time="08:45:22" end_time="17:15:22" >
<attributes>
<attribute name="innerParis" class="java.lang.Boolean" >true</attribute>
</attributes>
</activity>
<leg mode="pt" dep_time="17:15:22" trav_time="00:07:05">
<route type="links" start_link="138852" end_link="189898" trav_time="00:07:05" distance="4604.544053407517" vehicleRefId="7262234">138852</route>
</leg>
<activity type="outside" link="189898" facility="outside_249" x="648729.9598002436" y="6866057.250182923" end_time="17:20:35" >
</activity>
</plan>
</person>
<person id="102">
<attributes>
<attribute name="age" class="java.lang.Integer" >53</attribute>
</attributes>
<plan score="-0.38" selected="yes">
<activity type="inside" link="81312" facility="outside_208" x="649324.9906891582" y="6866581.699995641" end_time="08:22:00" >
</activity>
<leg mode="bike" dep_time="08:22:00" trav_time="00:10:13">
<route type="links" start_link="81312" end_link="138852" trav_time="00:10:13" distance="6046.54932060571" vehicleRefId="7262234">81312</route>
</leg>
<activity type="work" link="138852" facility="38407" x="651680.6" y="6863892.5" start_time="08:45:22" end_time="17:15:22" >
<attributes>
<attribute name="innerParis" class="java.lang.Boolean" >true</attribute>
</attributes>
</activity>
<leg mode="bike" dep_time="08:22:00" trav_time="00:10:13">
<route type="links" start_link="81312" end_link="138852" trav_time="00:10:13" distance="6046.54932060571" vehicleRefId="7262234">81312</route>
</leg>
<activity type="work" link="138852" facility="38407" x="651680.6" y="6863892.5" start_time="08:45:22" end_time="17:15:22" >
<attributes>
<attribute name="innerParis" class="java.lang.Boolean" >true</attribute>
</attributes>
</activity>
<leg mode="pt" dep_time="17:15:22" trav_time="00:07:05">
<route type="links" start_link="138852" end_link="189898" trav_time="00:07:05" distance="4604.544053407517" vehicleRefId="7262234">138852</route>
</leg>
<activity type="outside" link="189898" facility="outside_249" x="648729.9598002436" y="6866057.250182923" end_time="17:20:35" >
</activity>
</plan>
</person>
<person id="103">
<attributes>
<attribute name="age" class="java.lang.Integer" >53</attribute>
</attributes>
<plan score="-0.38" selected="yes">
<activity type="inside" link="81312" facility="outside_208" x="649324.9906891582" y="6866581.699995641" end_time="08:22:00" >
</activity>
<leg mode="bike" dep_time="08:22:00" trav_time="00:10:13">
<route type="links" start_link="81312" end_link="138852" trav_time="00:10:13" distance="6046.54932060571" vehicleRefId="7262234">81312</route>
</leg>
<activity type="shopping" link="138852" facility="38407" x="651680.6" y="6863892.5" start_time="08:45:22" end_time="17:15:22" >
<attributes>
<attribute name="innerParis" class="java.lang.Boolean" >true</attribute>
</attributes>
</activity>
<leg mode="bike" dep_time="08:22:00" trav_time="00:10:13">
<route type="links" start_link="81312" end_link="138852" trav_time="00:10:13" distance="6046.54932060571" vehicleRefId="7262234">81312</route>
</leg>
<activity type="work" link="138852" facility="38407" x="651680.6" y="6863892.5" start_time="08:45:22" end_time="17:15:22" >
<attributes>
<attribute name="innerParis" class="java.lang.Boolean" >true</attribute>
</attributes>
</activity>
<leg mode="pt" dep_time="17:15:22" trav_time="00:07:05">
<route type="links" start_link="138852" end_link="189898" trav_time="00:07:05" distance="4604.544053407517" vehicleRefId="7262234">138852</route>
</leg>
<activity type="outside" link="189898" facility="outside_249" x="648729.9598002436" y="6866057.250182923" end_time="17:20:35" >
</activity>
</plan>
</person>
</population>
Я намерен создать фрейм данных с тремя столбцами; activity type
, leg mode
и route distance
. Они должны быть заполнены приведенным ниже кодом.
Я пробую это с помощью следующего кода, но получаю следующее постоянное сообщение об ошибке:
import gzip
import xml.etree.ElementTree as ET
import pandas as pd
data = gzip.open('file.xml.gz', 'r')
root = ET.parse(data).getroot()
from collections import defaultdict
d = defaultdict(list)
for ent in root.findall('./person/plan[@selected="yes"]'):
if ent.name == 'activity':
d['type'].append(ent.get('type'))
elif ent.name == 'leg':
d['mode'].append(ent.get('mode'))
for place in ent.children:
if place.name=='route':
d['distance'].append(place.get('distance'))
coords=pd.DataFrame(d)
AttributeError: 'xml.etree.ElementTree.Element' object has no attribute 'children'
Я прочитал это и это, но не знаю, как применить это к моей проблеме.
Большое спасибо за вашу помощь!