我得到了一个TypeError:不是所有的参数在字符串格式化期间都被转换

2024-04-25 17:08:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我是新的编程,希望这只是一个简单的修复。除了我试图在序列中找到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列?你知道吗


Tags: 代码fromreadlinelenif编程countnot
1条回答
网友
1楼 · 发布于 2024-04-25 17:08:00

您为%提供了四个参数,但只有三个格式字段:

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代码应该使用^{}

print "{}\t{}\t{:.4f}\t{}".format(seqId, len(seq), gcPercent, N)

相关问题 更多 >