我试图解析DNA字符串。
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 :(得分:1)
我能够通过改变来解决问题
for p_id, s in samples.values()
来
for p_id, s in samples.items()
我还注意到p_id和s_id是不同的,它们本来是一样的。
答案 1 :(得分: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