我正在编写一个测验,在学生完成测验后,在保存分数时,我试图确保学生的最后三个分数保存到他们的名字中。如果他们之前没有完成过测验,则会在文本文件中为他们写入一个新行。
filename = (str(class_number) + 'txt')
with open(filename, 'a') as f:
f.write(str(name) + " : " + str(score) + '\n')
with open(filename) as f:
f.seek(0)
scores = f.readline()
from collections import deque
for line in scores:
name, score = line.rstrip('\n').split(',')
score= int(score)
if name not in filename:
filename[name] = deque(maxlen=3)
temp_q = filename[name]
temp_q.append(str(score))
filename[name] = temp_q
filehandle = open (filename, 'w')
for key,values in filename.iteritems():
filehandle.write(name + ',')
filehandle.write (','.join(list(values)) + '\n')
filehandle.close()
这是错误:
name, score = line.rstrip('\n').split(',')
Value error need more than 1 value to unpack
最佳答案
此错误消息表明您尝试读取的文件中没有 ,
字符。
关于python - 值错误: need more than one value to unpack,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36531369/