错误:<模块>中的文件“ xml_parser.py”第5行,out_file = sys.argv [2] IndexError:列表索引超出范围

时间:2019-02-22 21:23:27

标签: python xml parsing blast

这个xml解析器已经为我工作了2年,但突然停了下来,我不确定为什么。这是我尝试运行“ python xml_parser.py file_name.xml”时收到的消息。我什至用过去解析过的xml文档来完成它,但是没有用。

以下是警告:

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

这是我的脚本:

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()

1 个答案:

答案 0 :(得分:0)

sys.argv[0]是您的脚本,而sys.argv[1]是xml文件(in_file)的名称。它希望您还指定out_file的名称作为第二个参数(即sys.argv[2]

所以这样的事情应该做:

python xml_parser.py file_name.xml out_file