2012-04-27 54 views
1
發電機

我使用Python和這裏有一塊我的代碼:錯誤:必須海峽,沒有在Python

wp = open(outfile, 'w') 
fields = line.split('\t') 
gene_ID = fields[0] 
chr = fields[1] 
strand = fields[2] 
start = int(fields[3]) 
end = int(fields[4]) 
bc = {'A': 'T', 'C': 'G', 'G': 'C', 'T': 'A', 'N':'N'}  
if strand == '+': 
    wp.write(chr_string[start:end]) 
if strand == '-': 
    newstart, newend = -(start + 1), -(end + 1) 
    wp.write(bc[base.upper()] for base in chr_string[newstart:newend]) <--error pointed at this line 

當我嘗試運行我的整個代碼,我得到以下信息:

TypeError: must be str, not generator 

有沒有人知道我的代碼是什麼毛病提示呢?

+2

嗯,這是相當不言自明的:你正在傳遞一個生成器表達式到'write',當它期待一個字符串時... – Cameron 2012-04-27 17:38:33

回答

5

bc[base.upper()] for base in chr_string[newstart:newend]是一個生成器表達式。 你需要通過例如一個字符串創建一個字符串。 join方法:''.join(c[base.upper()] for base in chr_string[newstart:newend])

+1

謝謝!這就是我想念的!我是否還需要在鏈=='+'上執行此操作? – 2012-04-27 17:42:36

+0

不需要'chr_string [start:end]'是一個字符串的切片,因此也是一個字符串。 – 2012-04-27 18:34:22

相關問題