执行python时索引错误

时间:2017-07-28 02:34:19

标签: python index-error

我是python的初学者,目前正在执行一段代码,但它引发了以下错误。我尽力解决但却做不到。请帮我。代码如下。

    continue
    num_snps_skipped += 1
    samp_id = sline[id_index]
    ref_allele = sline[ref_allele_index]
    tum_allele = sline[tum_allele_index]
    snp = ref_allele + tum_allele
    if not snp in transitions:
        snp = nucleotide_complement[ref_allele] + nucleotide_complement[tum_allele]
    ref_trinuc = sline[ref_tri_index]
    if ref_trinuc == "NA":
        print "Warning: Reference allele not available on "+\
                "line %d; skipping line"%line_number
        continue
    if not ref_trinuc[1] == snp[0]:
        print "Warning: Reference allele does not match reference "+\
                "trinucleotide; skipping line %d"%line_number**
        continue
    snp_with_ctx = ref_trinuc[0] + snp + ref_trinuc[2]
    if not samp_id in signatures:
        signatures[samp_id] = [0 for i in substitution_order]
    if snp_with_ctx not in substitution_order:
        print "Warning: substitution on line " + \
                "%d is %s, not "%(line_number,snp_with_ctx) + \
                "found among possible substitutions. Skipping line."

我收到如下错误

    Traceback (most recent call last):
  File "main.py", line 59, in <module>
    signatures = signature.make(args.in_file, substitution_order=stratton['substitution_order'], out_path = args.spectrum_output)
  File "/home/ateeqanees/Mutation/centos/mutation-signatures-master/signature.py", line 73, in make
    if not ref_trinuc[1] == snp[0]:
IndexError: string index out of range

请帮帮我。这将是非常有帮助的。

非常感谢!!! DAV

1 个答案:

答案 0 :(得分:0)

您的ref_trinuc字符串很可能包含一个或零个字符。这就是if not ref_trinuc[1] == snp[0]:给出索引错误的原因,因为它试图抓住第二个字符。

在该行之前尝试打印ref_trinuc以查看它的内容。

如果ref_trinuc有时短于2个字符是正常的,请尝试以下语句:

if (len(ref_trinuc) > 1) and (not ref_trinuc[1] == snp[0]):

因为如果长度为1或0,则在这种情况下不会尝试后半部分并且不会抛出索引错误。