Unpickled object does not match original
我的程序无法解开我的数据,其中pickle.load(f)与pickle.dump(object,f)不匹配。
我的问题是我在下面的代码中出错了,因为我已经尝试过各种各样的代码
我的代码上面列出了相应错误的文件模式:
f = open(home +'/。GMouseCfg','ab +')
出:他们是不同的
f = open(home +'/。GMouseCfg','ab +',encoding ='utf-8')
ValueError:二进制模式不接受编码参数
f = open(home +'/。GMouseCfg','a +')
TypeError:必须是str,而不是字节
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | import abc, pprint from evdev import ecodes as e from os.path import expanduser try: import cPickle as pickle except: import pickle class Command(object): __metaclass__ = abc.ABCMeta @abc.abstractmethod def set(self, data): """set data used by individual commands""" return @abc.abstractmethod def run(self): """implement own method of executing data of said command""" return class KeyCommand(Command): def __init__(self, data): self.data = data def set(data): self.data = data def run(self): pass def __str__(self): return data class SystemCommand(Command): def __init__(self, data): self.data = data def set(data): self.data = data def run(self): pass def __str__(self): return data if __name__ == '__main__': ids = [2,3,4,5,6,7,8,9,10,11,12] home = expanduser('~') f = open(home + '/.GMouseCfg','a+') f.seek(0) commands = list() commands.append(KeyCommand({3:[e.KEY_RIGHTCTRL,e.KEY_P]})) commands.append(SystemCommand({5:['gedit','./helloworld.txt']})) pickle.dump(commands,f) f.seek(0) commands2 = pickle.load(f) if commands == commands2: print('They are the same') else: print('They are different') |
我已经做了很多阅读python docs for pickles和file io但是我无法辨别为什么我的原始对象和未经修饰的对象之间存在差异
在酸洗和去除后,显然
这意味着
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | class KeyCommand(Command): ... def __eq__(self, other): return self.data == other.data def __ne__(self, other): return self.data != other.data ... class SystemCommand(Command): ... def __eq__(self, other): return self.data == other.data def __ne__(self, other): return self.data != other.data ... |