【发布时间】: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
非常感谢您的帮助。
【问题讨论】: