【发布时间】:2016-04-20 23:52:30
【问题描述】:
我正在阅读一个包含超过 106.000 个条目的大型 XML。每个条目都是一组具有大量信息的研究人员。我做了一个阅读功能。
但是,如果在任何时候,任何信息丢失,我会得到
IndexError: 子索引超出范围
有没有办法告诉程序在孩子失踪时忽略?
由于数据的多样性,对于每个收集的数据,我可能会有不同大小的信息。
每次检查可能不是一个好主意,例如:
if root[0]0][0][0]:
tot_nac_2011 = int(root[0][0][0][0].attrib['TOT-BIBL-PERIODICO-NAC']
这是我的代码
from xml.etree import ElementTree
extended = ElementTree.parse('0000301510136952_2014_estendido.xml')
def read_researcher(extended):
root = extended.getroot()
members = []
for each in range(len(root[0])):
group_id = root.attrib['NRO-ID-GRUPO']
research_id = root[0][each].attrib['NRO-ID-CNPQ']
name = root[0][each].attrib['NOME-COMPLETO']
tit = root[0][each].attrib['TITULACAO-MAXIMA']
sex = root[0][each].attrib['SEXO']
tot_nac_2011 = int(root[0][each][0][0].attrib['TOT-BIBL-PERIODICO-NAC'])
tot_nac_2014 = int(root[0][each][0][3].attrib['TOT-BIBL-PERIODICO-NAC'])
tot_int_2011 = int(root[0][each][0][0].attrib['TOT-BIBL-PERIODICO-INT'])
tot_int_2014 = int(root[0][each][0][3].attrib['TOT-BIBL-PERIODICO-INT'])
tot_bbl_2011 = int(root[0][each][0][0].attrib['TOT-BIBL'])
tot_bbl_2014 = int(root[0][each][0][3].attrib['TOT-BIBL'])
members.append(researchers.Researcher(group_id, research_id, name, tit, sex, tot_nac_2011, tot_nac_2014, tot_int_2011, tot_int_2014, tot_bbl_2011, tot_bbl_2014))
return members
【问题讨论】:
-
您能否提供一些研究人员的示例 XML(请避免发布敏感信息)?谢谢。
-
这里是链接[链接] (dropbox.com/s/farljl3u0basvfx/…)
标签: python xml python-3.x xml-parsing