Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python ValueError:太多的值太过解包(预期为2个)_Python - Fatal编程技术网

Python ValueError:太多的值太过解包(预期为2个)

Python ValueError:太多的值太过解包(预期为2个),python,Python,我正在尝试解析DNA字符串 input.txt包含: 罗莎琳>罗莎琳>罗莎琳>罗莎琳>罗莎琳>罗莎琳>罗莎琳>罗莎琳>罗莎琳>罗莎琳>罗莎琳>罗莎琳>罗莎琳>罗莎琳>罗莎琳>罗莎琳>罗莎琳 代码是: f = open('input.txt', 'r') raw_samples = f.readlines() f.close() samples = {} cur_key = '' for elem in raw_samples: if elem[0] == '>':

我正在尝试解析DNA字符串

input.txt包含:

罗莎琳>罗莎琳>罗莎琳>罗莎琳>罗莎琳>罗莎琳>罗莎琳>罗莎琳>罗莎琳>罗莎琳>罗莎琳>罗莎琳>罗莎琳>罗莎琳>罗莎琳>罗莎琳>罗莎琳

代码是:

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)`
我不断得到错误:

文件“C:/Python34/test.py”,第18行,在
对于p_id,s在samples.values()中:
ValueError:要解压缩的值太多(应为2个)

我能通过改变环境来解决这个问题
用于样本中的p_id,s.values()
用于示例.items()中的p\U id

我还注意到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)))

我希望它有用D

您希望在这里获得
p_id
s
什么?您正试图将值(字符串)解压为两个变量,但只有一个字符串包含两个以上的字符。你的意思是使用
samples.items()
来代替吗?
对于samples.values()中的p_id,s迭代一个一维列表,就像它是一个二维列表一样。另外,我不确定使用字典连接续行是读取FASTA文件的正确方法。最后但并非最不重要的一点,
suid
应该是什么意思?@Martijn Pieters你还有别的方法吗?
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)))