简体   繁体   中英

Error: File “xml_parser.py”, line 5, in <module> out_file = sys.argv[2] IndexError: list index out of range

This xml parser has worked for me for 2 years, but suddenly stopped, and I'm not sure why. This is the message I get when I try to run 'python xml_parser.py file_name.xml'. I've even done it with xml documents that have parsed in the past, to no avail.

Here is the warning:

File "xml_parser.py", line 5, in <module>
    out_file = sys.argv[2]
IndexError: list index out of range

Here is my script:

import sys
import re

in_file = sys.argv[1]
out_file = sys.argv[2]

output = open(out_file,'w')
print >> output, 'query'+'\t'+'query_length'+'\t'+'alignment_length'+'\t'+'identity'+'\t'+'hit_def'+'\t'+'hit_from'+'\t'+'hit_to'+'\t'+'hit_accession'+'\t'+'e-value'

with open(in_file,'r') as xml:
    for i in xml:
        if re.search('<Iteration_query-def>', i) != None:
            i = i.split('>',1)[-1]
            i = i.split('<', 1)[-2]
            query_def = i
        if re.search('<Iteration_query-len>', i) != None:
            i = i.split('>',1)[-1]
            i = i.split('<', 1)[-2]
            query_len = i
        if re.search('No hits found', i) != None:
            i = i.split('>',1)[-1]
            i = i.split('<', 1)[-2]
            print >> output, query_def+'\t'+i
        if re.search('<Hit_def>', i) != None:
            i = i.split('>',1)[-1]
            i = i.split('<', 1)[-2]
            hit_def = i
        if re.search('<Hit_accession>', i) != None:
            i = i.split('>',1)[-1]
            i = i.split('<', 1)[-2]
            hit_acc = i       
        if re.search('<Hsp_evalue>', i) != None:
            i = i.split('>',1)[-1]
            i = i.split('<', 1)[-2]
            e_val = i
        if re.search('<Hsp_hit-from>', i) != None:
            i = i.split('>',1)[-1]
            i = i.split('<', 1)[-2]
            hit_from = i
        if re.search('<Hsp_hit-to>', i) != None:
            i = i.split('>',1)[-1]
            i = i.split('<', 1)[-2]
            hit_to = i
        if re.search('<Hsp_identity>', i) != None:
            i = i.split('>',1)[-1]
            i = i.split('<', 1)[-2]
             hsp_identity = i
        if re.search('<Hsp_align-len>', i) != None:
            i = i.split('>',1)[-1]
            i = i.split('<', 1)[-2]
            align_len = i
            print >> output, query_def+'\t'+query_len+'\t'+align_len+'\t'+hsp_identity+'\t'+hit_def+'\t'+hit_from+'\t'+hit_to+'\t'+hit_acc+'\t'+e_val 

output.close()

sys.argv[0] is your script, and sys.argv[1] is the name of the xml file (in_file). It expects you to specify also the name of the out_file as second argument (ie sys.argv[2] )

So something like this should do:

python xml_parser.py file_name.xml out_file

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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