KeyError:'\ n'而在Python 2.7.10中取消

时间:2016-10-05 21:26:46

标签: python pickle

我有一个activity_list=[('text 1', 'text 2', "('No recent activity',)")........]形式的元组列表,我将其作为

文件腌制而成
with open('file.dat', 'wb') as writefile:
    pickle.dump(activity_list, writefile, protocol=pickle.HIGHEST_PROTOCOL)

然而,当我尝试使用

取消此文件时
if os.path.isfile('file.dat') and os.stat('file.dat').st_size!=0:
        with open('file.dat', 'rb') as myfile:
                activitydata=pickle.load(myfile)
                print activitydata

我收到以下错误:

`Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 1536, in __call__
    return self.func(*args)
  File "C:\Python27\CMS downloader\cms-dl\gui.py", line 117, in show_last
    activitydata=pickle.load(myfile)
  File "C:\Python27\lib\pickle.py", line 1378, in load
    return Unpickler(file).load()
  File "C:\Python27\lib\pickle.py", line 858, in load
    dispatch[key](self)
KeyError: '\n'

确实是出现此错误的原因,我该如何解决?

代码: `

import pickle
import os
activity_list=[('Activity since Thursday, 6 October 2016, 3:18 AM', 'CS/ECE/EEE/INSTR F215 DIGITAL DESIGN LS1', "('No recent activity',)"), ('Activity since Thursday, 6 October 2016, 3:18 AM', 'ECE/EEE/INSTR F211 ELECTRICAL MACHINES LS1', "('No recent activity',)")]
with open('activity.dat', 'wb') as writefile:
    pickle.dump(activity_list, writefile, protocol=pickle.HIGHEST_PROTOCOL)
if os.path.isfile('activity.dat') and os.stat('activity.dat').st_size!=0:
    with open('activity.dat', 'rb') as myfile:
        print repr(myfile.read())
        activitydata=pickle.load(myfile)
        print activitydata

” 输出: “

"\x80\x02]q\x00(U0Activity since Thursday, 6 October 2016, 3:18 AMq\x01U(CS/ECE/EEE/INSTR F215 DIGITAL DESIGN LS1q\x02U\x17('No recent activity',)q\x03\x87q\x04U0Activity since Thursday, 6 October 2016, 3:18 AMq\x05U*ECE/EEE/INSTR F211 ELECTRICAL MACHINES LS1q\x06U\x17('No recent activity',)q\x07\x87q\x08e."

Traceback (most recent call last):
  File "C:/Python27/CMS downloader/cms-dl/lol.py", line 9, in <module>
    activitydata=pickle.load(myfile)
  File "C:\Python27\lib\pickle.py", line 1378, in load
    return Unpickler(file).load()
  File "C:\Python27\lib\pickle.py", line 858, in load
    dispatch[key](self)
  File "C:\Python27\lib\pickle.py", line 880, in load_eof
    raise EOFError
EOFError

`

0 个答案:

没有答案