【发布时间】:2015-09-03 18:17:29
【问题描述】:
我有 2 个班级:小组和学生。我创建了不同组的列表,每个组都有包含学生列表的属性。然后我以这种方式使用pickle将它保存在文件中:
tfile = open( 'test', "w" )
pickle.dump(encodedList, tfile)
tfile.close()
这 3 行运行良好。再次启动程序后,我想从列表中的文件中获取所有这些信息,我根据许多这样的教程来做:
encodedList = []
try:
with open('test') as file:
tfile = open( 'test', "r" )
encodedList = pickle.load( tfile )
except IOError:
tfile = open( 'test', "w" )
pickle.dump( encodedList, tfile )
tfile.close()
但是 Programm 在此处崩溃并给出下一个错误:
我尝试以不同的类似方式从文件中读取此列表,但此错误始终相同,您能帮帮我吗?
【问题讨论】:
-
你为什么用
with打开一次,然后又在with块内打开? -
还有Group指的是什么,你pickle了什么?
-
@PadraicCunningham 我删除了第二个开口,但没有帮助。
-
@PadraicCunningham Group 只是类的名称,然后我创建它的实例并将它们添加到列表中。
-
如果在同一个文件中的类定义你正在做unpickling?
标签: python list python-2.7 pickle