gpt4 book ai didi

Python 键错误 : '\n'

转载 作者:太空宇宙 更新时间:2023-11-04 05:40:07 28 4
gpt4 key购买 nike

我使用下面的脚本从 fastq 文件中提取分子条形码。但是,我不断收到以下关键错误。

File "extractMolecularBarcode.py", line 42, in <module>
dicoBarcode[barcode] += 1
KeyError: '\n'

我知道关键错误意味着字典中没有定义某些东西,但我无法找出问题所在。你能帮忙吗?非常感谢!

这是脚本:

import sys, itertools


iFastq=open(sys.argv[1], 'r')
oFastq=open(sys.argv[2], 'w')
oBarcode=open(sys.argv[3], 'w')
oLigation=open(sys.argv[4], 'w')

dicoBarcode={}
dicoLigation={}
nct='ACTGN'
for barcode in list(itertools.product(nct, repeat=6)):
dicoBarcode["".join(barcode)] = 0
dicoLigation["".join(barcode)] = 0


header= iFastq.readline().rstrip()
while header != '':
totseq= iFastq.readline()
plus = iFastq.readline()
qual = iFastq.readline()
barcode = totseq[0:6]
ligation = totseq[3:9]
seq = totseq[6:]
oFastq.write(header.split(" ")[0]+'_MolecularBarcode:'+barcode+' '+header.split(" ")[1]+'\n')
oFastq.write(seq)
oFastq.write(plus)
oFastq.write(qual[6:])
header= iFastq.readline().rstrip()

dicoBarcode[barcode] += 1
if len(seq) >= 4 :
dicoLigation[ligation] += 1

for barcode, times in dicoBarcode.items():
oBarcode.write("%s\t%s\n" % (barcode, str(times)))

for ligation, times in dicoLigation.items():
oLigation.write("%s\t%s\n" % (ligation, str(times)))

最佳答案

你的文件中有一个换行符,当你使用

 dicoBarcode[barcode] += 1

条码值是换行符 或 '\n' 会导致错误!

您可以通过提供默认值来克服它:

discoBarcode.get(barcode,YOURDEFAULT)

或者您可以先删除换行符然后再处理文件 ;)

yourfile.readline().rstrip("\n")

关于Python 键错误 : '\n' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34262548/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com