简体   繁体   English

ValueError:太多的值解包(预期2)

[英]ValueError: too many values too unpack (expected 2)

I'm trying to parse DNA strings. 我正在尝试解析DNA字符串。

The input.txt contains: input.txt包含:

Rosalind_6404CCTGCGGAAGATCGGCACTAGAATAGCCAGAACCGTTTCTCTGAGGCTTCCGGCCTTCCCTCCCACTAATAATTCTGAGG>Rosalind_5959CCATCGGTAGCGCATCCTTAGTCCAATTAAGTCCCTATCCAGGCGCTCCGCCGAAGGTCTATATCCATTTGTCAGCAGACACGC>Rosalind_0808CCACCCTCGTGGTATGGCTAGGCATTCAGGAACCGGAGAACGCTTCAGACCAGCCCGGACTGGGAACCTGCGGGCAGTAGGTGGAAT 罗莎琳德(640)

The code is: 代码是:

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)`

I keep getting the error: 我不断收到错误:

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

I was able to solve the problem by changing for p_id, s in samples.values() to for p_id, s in samples.items() 我可以通过将for p_id, s in samples.values()for p_id, s in samples.items()更改for p_id, s in samples.values()for p_id, s in samples.items()来解决问题

I also noticed that p_id and s_id were different, they were meant to be the same. 我还注意到p_id和s_id不同,它们的含义是相同的。

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)))

I hope it's useful. 我希望它有用。 :D :D

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM