Decreasing the size of cPickle objects

前端 未结 3 724
悲哀的现实
悲哀的现实 2020-12-07 15:37

I am running code that creates large objects, containing multiple user-defined classes, which I must then serialize for later use. From what I can tell, only pickling is ver

3条回答
  •  没有蜡笔的小新
    2020-12-07 16:38

    You can combine your cPickle dump call with a zipfile:

    import cPickle
    import gzip
    
    def save_zipped_pickle(obj, filename, protocol=-1):
        with gzip.open(filename, 'wb') as f:
            cPickle.dump(obj, f, protocol)
    

    And to re-load a zipped pickled object:

    def load_zipped_pickle(filename):
        with gzip.open(filename, 'rb') as f:
            loaded_object = cPickle.load(f)
            return loaded_object
    

提交回复
热议问题