import cPickle
sobject = cPickle.dumps(object, 1)
print len(sobject)
存储序列化对象
import pickle
@staticmethod
def swrite(obj, file):
output = open(file, 'wb')
pickle.dump(obj, output)
output.close()
@staticmethod
def sread(file):
try:
pk_file = open(file, 'rb')
obj = pickle.load(pk_file)
return obj
except EOFError:
return None
except IOError:
return None
本文介绍了使用Python的pickle模块来实现对象的序列化和存储的方法。通过具体示例展示了如何将对象序列化为字符串以及如何将序列化的对象写入文件和从文件中读取。此外还讨论了在读取过程中可能遇到的异常处理。
1980

被折叠的 条评论
为什么被折叠?



