简体   繁体   English

TypeError:列表索引必须是整数,而不是xmltodict的str:

[英]TypeError: list indices must be integers, not str with xmltodict:

I have got this XML file: 我有这个XML文件:

<?xml version="1.0"?>
<toolbox tool_path="/galaxy/main/shed_tools">
<section id="snpeff" name="snpEff" version="">
  <tool file="toolshed.g2.bx.psu.edu/repos/pcingola/snpeff/c052639fa666/snpeff/snpEff_2_1a/snpEff_2_1a/galaxy/snpSift_filter.xml" guid="toolshed.g2.bx.psu.edu/repos/pcingola/snpeff/snpSift_filter/1.0">
      <tool_shed>toolshed.g2.bx.psu.edu</tool_shed>
        <repository_name>snpeff</repository_name>
        <repository_owner>pcingola</repository_owner>
        <installed_changeset_revision>c052639fa666</installed_changeset_revision>
        <id>toolshed.g2.bx.psu.edu/repos/pcingola/snpeff/snpSift_filter/1.0</id>
        <version>1.0</version>
    </tool>
    <tool file="toolshed.g2.bx.psu.edu/repos/pcingola/snpeff/c052639fa666/snpeff/snpEff_2_1a/snpEff_2_1a/galaxy/snpEff.xml" guid="toolshed.g2.bx.psu.edu/repos/pcingola/snpeff/snpEff/1.0">
      <tool_shed>toolshed.g2.bx.psu.edu</tool_shed>
        <repository_name>snpeff</repository_name>
        <repository_owner>pcingola</repository_owner>
        <installed_changeset_revision>c052639fa666</installed_changeset_revision>
        <id>toolshed.g2.bx.psu.edu/repos/pcingola/snpeff/snpEff/1.0</id>
        <version>1.0</version>
    </tool>
    <tool file="toolshed.g2.bx.psu.edu/repos/gregory-minevich/check_snpeff_candidates/22c8c4f8d11c/check_snpeff_candidates/checkSnpEffCandidates.xml" guid="toolshed.g2.bx.psu.edu/repos/gregory-minevich/check_snpeff_candidates/check_snpeff_candidates/1.0.0">
      <tool_shed>toolshed.g2.bx.psu.edu</tool_shed>
        <repository_name>check_snpeff_candidates</repository_name>
        <repository_owner>gregory-minevich</repository_owner>
        <installed_changeset_revision>22c8c4f8d11c</installed_changeset_revision>
        <id>toolshed.g2.bx.psu.edu/repos/gregory-minevich/check_snpeff_candidates/check_snpeff_candidates/1.0.0</id>
        <version>1.0.0</version>
    </tool>
</section>
...

I have tried to parse the above file in the following way: 我尝试通过以下方式解析以上文件:

import xmltodict

# wget -c https://raw.githubusercontent.com/galaxyproject/usegalaxy-playbook/c55aa042825fe02ef4a02d958eb811adba8ea45f/files/galaxy/usegalaxy.org/var/shed_tool_conf.xml

if __name__ == '__main__':

    with open('tests/shed_tool_conf.xml') as fd:
        doc = xmltodict.parse(fd.read())
        tools_section = doc['toolbox']['section']['@name']
        print tools_section

However, I have got the following error: 但是,出现以下错误:

Traceback (most recent call last):
  File "importTools2Galaxy.py", line 15, in <module>
    tools_section = doc['toolbox']['section']['@name']
TypeError: list indices must be integers, not str

What did I do wrong? 我做错什么了?

This is because doc['toolbox']['section'] returns a list of sections so you need to iterate over each section to get @name value. 这是因为doc['toolbox']['section']返回一个节list ,因此您需要遍历每个节以获得@name值。 You may want check if @name is in given section. 您可能要检查@name是否在给定的部分中。 For that you may want to use .get instead of ['@name'] 为此,您可能需要使用.get而不是['@name']

with open('tests/shed_tool_conf.xml') as fd:
        doc = xmltodict.parse(fd.read())
        for section in doc['toolbox']['section']:
            tools_section = section.get('@name')
        print tools_section

Your XML has many section elements, you should do something like 您的XML有很多section元素,您应该执行以下操作

tools_section = doc['toolbox']['section'][0]

where 0 is the index of the section you want to read. 其中0是您要阅读的部分的索引。 If the index is not fixed, you can iterate over them like for section in doc['toolbox']['section']: ... and stop at the section whose contents match your criteria... or just do something with each of the sections. 如果索引不是固定的,则可以像for section in doc['toolbox']['section']: ...那样遍历它们for section in doc['toolbox']['section']: ...然后在符合您的条件的那部分停下来...或者对每个部分都做些什么部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM