码迷,mamicode.com
首页 > 编程语言 > 详细

python pickle 序列化类

时间:2016-09-22 20:02:37      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:

python pickle 序列化类

# coding:utf-8
try:
    import cPickle as pickle
except ImportError:
    import pickle


class 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 = 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 序列化类

标签:

原文地址:http://www.cnblogs.com/localpython/p/5897564.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!