首页 > 代码库 > 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 序列化类