Python索引器错误:在vcf中查找SNP时列表索引超出范围
大家好,我应该使用Python脚本从vcf文件中的csv文件中识别指定位置的可能SNP。Python索引器错误:在vcf中查找SNP时列表索引超出范围,python,csv,error-handling,range,vcf-vcard,Python,Csv,Error Handling,Range,Vcf Vcard,大家好,我应该使用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 scri
我刚开始使用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
例如,如果
i='aa'
并且您执行line=i.split(“,”
这意味着line=['aa']
,那么当您执行line[2]+“\u”+line[3]
时,您将得到一个索引器,因为line
没有第二和第三个元素
使用try/except
或重新思考代码的逻辑
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)