TypeError: index indices must be integers, not str with xmltodict:

I have this xml file:

<?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 tried to parse the above file as follows:

 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 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 have I done wrong?

+4
source share
2 answers

There are many section elements in your XML, you should do something like

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

where 0 is the index of the section you want to read. If the index is not fixed, you can for section in doc['toolbox']['section']: ... over them as for section in doc['toolbox']['section']: ... and stop at a section whose contents meet your criteria ... or just do something with each of sections.

+2
source

This is because doc['toolbox']['section'] returns a list sections, so you need to @name over each section to get the @name value. You can check if @name in this section. For this you can use .get instead of ['@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 
+2
source

All Articles