Python-Pickle Str-Int转换

时间:2015-09-20 20:14:24

标签: python pickle

所以我正在完成这个任务分配工作,以提高我对如何在python中进行pickle的知识。长话短说,任务是创建一个数学测验,将数据保存到外部文件夹,然后取消数据并对其进行排序(按字母顺序,平均值,从最高到最低等)。问题我是为了得分要保存它必须作为字符串pickled,而不是整数strscore = str(score) 我如何在腌制后将分数转换回整数,我已经尝试scores = pickle.load(datafile) intscore = int(scores)但我收到了错误TypeError: 'int' object is not iterable,请帮助,我需要的是将得分值作为整数返回,这样我就可以加起来得分,谢谢

修改

scorestr = str(score)
newscore = ""
if score < 10:
    newscore = "0"+(scorestr)
else:
    newscore = "010"

这是节目中保存测验得分的部分

1 个答案:

答案 0 :(得分:0)

据我了解,你这样做是为了加载文件:

scores = pickle.load(datefile)
print(type(scores))
intscore = int(scores)

如果print(type(scores))返回类'str',则表示当您加载日期文件时,它已经返回一个字符串,因此问题很可能在pickle.dump()

您能否告诉我们您执行pickle.dump()的代码部分?