【问题标题】:Python IndexError: list index out of range while finding SNPs in vcfPython IndexError:在 vcf 中查找 SNP 时列出超出范围的索引
【发布时间】:2015-12-14 10:45:32
【问题描述】:

大家好,我应该使用 Python 脚本从 vcf 文件中的 csv 文件中识别指定位置的可能 SNP。
我刚开始使用 python,遗憾的是我总是收到以下错误:

 Traceback (most recent call last):
 File "getSNPs.py", line 20, in <module>    oo = line[2] + "_" +
 line[3]
 IndexError: list index out of range from the following script
 !/bin/python usage: python getSNPs.py your.vcf PhenoSNPs.csv

代码:

import sys
import gzip

SNPs = {}

for i in gzip.open(sys.argv[1], "r"):
    if '#' not in i:
        line = i.split("\t")
        oo = line[0] + "_" + line[1]
        SNPs[oo] = i

pp = sys.argv[1] + ".captureSNPs"

out = open(pp, "w")

for i in open(sys.argv[2], "r"):
    line = i.split(",")
    oo = line[2] + "_" + line[3]
    try:
        out.write(SNPs[oo])
    except KeyError:
        ow = line[2] + "\t" + line[3] + "\t" + "not covered" + "\n"
        out.write(ow)   

【问题讨论】:

    标签: python csv error-handling range vcf-vcard


    【解决方案1】:

    例如,如果i = 'aa' 而你使用line = i.split(","),则意味着line = ['aa'],那么当你使用line[2] + "_" + line[3] 时,你会得到IndexError,因为line 没有第二和第三元素。

    使用try/except 或重新考虑代码的逻辑。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-25
      • 2017-04-05
      • 2012-07-15
      • 2013-06-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多