我是编程新手,希望这只是一个简单的修复。除了我在序列中找到N
的数量时,一切正常。这是我正在使用的代码:
from __future__ import division
print "Sequence Information"
f = open('**,fasta','r')
while True:
seqId = f.readline()
#Check if there are still lines
if not seqId: break
seqId = seqId.strip()[1:]
seq = f.readline()
# Find the %GC
gcPercent = (( seq.count('G') + seq.count('g') + seq.count('c') + seq.count('C') ) / (len( seq )) *100)
N = (seq.count('N') + 1)
print "%s\t%d\t%.4f" % (seqId, len( seq ), gcPercent, N)
我一直收到以下错误:
Traceback (most recent call last):
File "length", line 20, in <module>
print "%s\t%d\t%.4f" % (seqId, len( seq ), gcPercent, N)
TypeError: not all arguments converted during string formatting
如何制作,以便将N
的值添加到第4列?
答案 0 :(得分:2)
您向%
提供了四个参数,但只有三个格式字段:
print "%s\t%d\t%.4f" % (seqId, len( seq ), gcPercent, N)
# ^1 ^2 ^3 ^1 ^2 ^3 ^4
Python要求每个参数都有一个格式字段,如下所示:
print "%s\t%d\t%.4f\t%d" % (seqId, len( seq ), gcPercent, N)
当然,现代Python代码应该使用str.format
代替:
print "{}\t{}\t{:.4f}\t{}".format(seqId, len(seq), gcPercent, N)