-1

我有一个 xml 文件,我必须通过它来检索 xml 文档。下面是我拥有的 xml 文档。

-<orcid-message>
   -<orcid-profile type="user">
      -<orcid-activities>
         -<orcid-works>
            -<orcid-work put-code="23938140" visibility="public">
               -<work-contributors>
                  -<contributor>
                       -<credit-name visibility="public">Tania Maes</credit-name>
                  -<contributor>
                       -<credit-name visibility="public">Francisco Avila Cobos</credit-name>
                  -<contributor>
                       -<credit-name visibility="public">Franco Liala Manus</credit-name>

我想检索贡献者姓名:到目前为止我已经尝试过:

contributors_name = (doc['orcid-message']['orcid-profile']
                        ['orcid-activities']['orcid-works']
                        ['orcid-work']['work-contributors']
                        ['contributor']['credit-name']  )

print(contributors_name)

请告诉我哪里出错了。谢谢你。

4

1 回答 1

0

TypeError: list indices must be integers, not str:我得到这个错误”

错误消息表明问题是由于 XML 包含多个contributor元素,因此您的代码最多['contributor']将返回一个列表,而该列表又不能['credit-name']像字典一样通过键(即)直接访问。您需要在列表中选择要从中获取credit-name的一项,例如从第一项:

contributors = doc['orcid-message']['orcid-profile'] \
    ['orcid-activities']['orcid-works'] \
    ['orcid-work']['work-contributors'] \
    ['contributor']
contributor_name = contributors[0]['credit-name']

或者您可以使用列表理解credit-name从所有贡献者那里获取信息:

contributors_name = [contrib['credit-name']['#text'] for contrib in contributors]
print(contributors_name)

输出 :

[u'Tania Maes', u'Francisco Avila Cobos', u'Franco Liala Manus']
于 2016-07-17T13:04:27.090 回答