2015-04-25 75 views
0

我想解析DNA字符串。ValueError:太多值太解壓(預計2)

的input.txt中包含:

Rosalind_6404CCTGCGGAAGATCGGCACTAGAATAGCCAGAACCGTTTCTCTGAGGCTTCCGGCCTTCCCTCCCACTAATAATTCTGAGG>Rosalind_5959CCATCGGTAGCGCATCCTTAGTCCAATTAAGTCCCTATCCAGGCGCTCCGCCGAAGGTCTATATCCATTTGTCAGCAGACACGC>Rosalind_0808CCACCCTCGTGGTATGGCTAGGCATTCAGGAACCGGAGAACGCTTCAGACCAGCCCGGACTGGGAACCTGCGGGCAGTAGGTGGAAT

的代碼是:

f = open('input.txt', 'r') 
raw_samples = f.readlines() 
f.close() 
samples = {} 
cur_key = '' 
for elem in raw_samples: 
    if elem[0] == '>': 
     cur_key = elem[1:].rstrip() 
     samples[cur_key] = '' 
    else: 
     samples[cur_key] = samples[cur_key] + elem.rstrip() 
print(samples) 
for p_id, s in samples.values(): 
    samples[s_id] = (s.count('G') + s.count('C'))*100 
print (samples)` 

我不斷收到錯誤:

File "C:/Python34/test.py", line 18, in <module> 
    for p_id, s in samples.values(): 
ValueError: too many values to unpack (expected 2) 
+0

你期望在這裏得到'p_id'和's'的結果嗎?您試圖將值(字符串)解壓縮爲兩個變量,但只有一個字符串中包含2個以上的字符。你的意思是使用'samples.items()'代替嗎? –

+0

'對於p_id,s在samples.values()中迭代一維列表,就好像它是一個二維列表。 –

+0

此外,我不確定使用字典串接續行是正確讀取FASTA文件的方法。 –

回答

1

我能夠通過改變來解決問題 for p_id, s in samples.values()for p_id, s in samples.items()

我還注意到,p_id和s_id不同,它們的意思是相同的。

1
import csv 
reader = csv.reader(open("input.txt"), delimiter=">", quotechar="'") 
dkeys = [item for item in next(reader) if item.strip()] 
dvalues = [(item.count('G')+item.count('C')*100) for item in dkeys] 
print(dict(zip(dkeys, dvalues))) 

我希望它很有用。 :D

相關問題