2014-12-03 111 views
-1

我是編程新手,希望這只是一個簡單的修復。除了當我試圖在序列中查找N的數目時,所有內容都正在工作。這是我使用的代碼:我得到一個TypeError:並非所有在字符串格式化過程中轉換的參數

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

爲什麼不加上anot她的'\ t%d'呢? – 2014-12-03 18:11:03

+0

你在字符串中有3個'%',但是在下面有4個值! – Kasramvd 2014-12-03 18:11:35

+0

是的,那工作。謝謝! *臉掌* – 2014-12-03 18:17:16

回答

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) 
相關問題