首页 > 代码库 > python pickle 序列化类
python pickle 序列化类
python pickle 序列化类
# coding:utf-8try: import cPickle as pickleexcept ImportError: import pickleclass PickleVar(object): __doc__ = ‘‘‘序列化操作类‘‘‘ def __init__(self): print (‘------------------------------------------------------‘) def dump(self, data, fileName): with open(fileName, "wb") as f: pickle.dump(data, f) def load(self, fileName): with open(fileName, ‘rb‘) as f: return pickle.load(f) def __del__(self): print (‘------------------------------------------------------‘)if __name__ == ‘__main__‘: # 生成随机数 data = http://www.mamicode.com/range(10) # print pickle.dumps(data) # print pickle.loads(pickle.dumps(data)) ‘‘‘注意:dump和dumps是有区别的,dump是保存到文件,而dumps只是生成序列化后的字符串‘‘‘ p = PickleVar() # 存入变量data p.dump(data, ‘file‘) print p.load(‘file‘)
python pickle 序列化类
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。