python 物件序列化並壓縮
阿新 • • 發佈:2018-11-25
python如果需要將物件存成檔案,方便以後(或放在其他地方)使用,可以使用序列化pickle,
參考另一篇隨筆:https://www.cnblogs.com/taoshiqian/p/9771786.html
但是一個較大的物件簡單序列化成檔案,可能會佔用很大空間,而此時物件中可能有很多重複資訊,完全可以壓縮儲存
因此,物件->檔案:序列化+壓縮,檔案->物件:解壓+反序列化,更省空間
import os, pickle, gzip def save_object_to_zip(objects, filename): if not os.path.exists(filename):if os.path.split(filename)[0]: # 需要資料夾 os.mkdir(os.path.split(filename)[0]) # 建立資料夾 os.mknod(filename) # 建立檔案 fil = gzip.open(filename, 'wb') pickle.dump(objects, fil) fil.close() def load_object_from_zip(filename): fil = gzip.open(filename, 'rb') while True:try: return pickle.load(fil) except EOFError: break fil.close() def test_pickle_and_zip(): L1 = [1, 2, 3, 4, 5] filename = 'L' save_object_to_zip(L1, filename) L2 = load_object_from_zip(filename) print(L2) print(type(L2)) L1 = 8 filename= 'int/L' save_object_to_zip(L1, filename) L2 = load_object_from_zip(filename) print(L2) print(type(L2)) L1 = {'a': 1, 'b': 2} filename = 'int/L' save_object_to_zip(L1, filename) L2 = load_object_from_zip(filename) print(L2) print(type(L2)) if __name__ == '__main__': test_pickle_and_zip()
#輸出 [1, 2, 3, 4, 5] <class 'list'> 8 <class 'int'> {'a': 1, 'b': 2} <class 'dict'>