Python 我得到一个TypeError:不是所有的参数都在字符串格式化期间转换
我是编程新手,希望这只是一个简单的修复。除了我试图在序列中找到N的数目外,其他一切都在工作。这是我正在使用的代码:Python 我得到一个TypeError:不是所有的参数都在字符串格式化期间转换,python,string,string-formatting,Python,String,String Formatting,我是编程新手,希望这只是一个简单的修复。除了我试图在序列中找到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
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的值加到第四列呢 您为%提供了四个参数,但只有三个格式字段:
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代码应该使用:
为什么不添加另一个\t%d呢?字符串中有3%,但后面有4个值!是的,成功了。谢谢掌心
print "{}\t{}\t{:.4f}\t{}".format(seqId, len(seq), gcPercent, N)