【问题标题】:How to systematically avoid or ignore child index out of range when reading large XML读取大型 XML 时如何系统地避免或忽略超出范围的子索引
【发布时间】: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

【问题讨论】:

标签: python xml python-3.x xml-parsing


【解决方案1】:

要回答您的具体问题:通过try/except 使用异常处理并处理从子元素中提取属性值时可能发生的相关错误。这有点像EAFP 编程风格。还有LBYL一个。

我还将使用中间字典改进代码以处理 Researcher 对象初始化参数,将 group_id 从循环下方移出,因为我们是从根元素中获取它的。

代码:

from xml.etree import ElementTree


extended = ElementTree.parse('0000301510136952_2014_estendido.xml')


def get_value(item, index, value):
    try:
        return int(item[index].attrib[value])
    except (IndexError, KeyError, AttributeError, ValueError):
        # TODO: log
        return None


def read_researcher(extended):
    root = extended.getroot()
    group_id = root.attrib['NRO-ID-GRUPO']

    members = []
    for item in root[0]:
        subitem = item[0]
        researcher = {
            "group_id": group_id,
            "research_id": item.attrib.get('NRO-ID-CNPQ'),
            "name": item.attrib.get('COMPLETO'),
            "tit": item.attrib.get('TITULACAO-MAXIMA'),
            "sex": item.attrib.get('SEXO'),
            "tot_nac_2011": get_value(subitem, 0, 'TOT-BIBL-PERIODICO-NAC'),
            "tot_nac_2014": get_value(subitem, 3, 'TOT-BIBL-PERIODICO-NAC'),
            "tot_int_2011": get_value(subitem, 0, 'TOT-BIBL-PERIODICO-INT'),
            "tot_int_2014": get_value(subitem, 3, 'TOT-BIBL-PERIODICO-INT'),
            "tot_bbl_2011": get_value(subitem, 0, 'TOT-BIBL'),
            "tot_bbl_2014": get_value(subitem, 3, 'TOT-BIBL'),
        }

        members.append(researchers.Researcher(**researcher))
    return members

【讨论】:

  • 谢谢。我是新手。有了这个,我还学到了 (**args) 并且我明白了 except: 可以返回一些东西。非常感谢。最好的,
猜你喜欢
  • 2018-06-18
  • 2016-07-22
  • 2016-02-12
  • 2023-03-09
  • 2023-04-07
  • 2023-03-27
  • 2021-11-14
  • 1970-01-01
  • 2019-12-11
相关资源
最近更新 更多