pickle模塊

pickle模塊

是python自帶的序列化模塊,相似於json模塊python

優勢:能夠支持python中全部的數據類型,能夠直接存取‘bytes’類型數據,存取速度快json

缺點(致命的):只能支持python使用,不能跨平臺code

import pickle
dic = {'name': '沈', 'age': 18}
pickle_str = pickle.dumps(dic)
print(pickle_str) # b'\x80\x03}q\x00(X\x04\x00\x00\x00nameq\x01X\x03\x00\x00\x00\xe6\xb2\x88q\x02X\x03\x00\x00\x00ageq\x03K\x12u.'
print(pickle.loads(pickle_str)) # {'name': '沈', 'age': 18}
set1 = {1,2, '沈'}
with open('user.pickle', 'wb') as f:
    pickle.dump(set1,f)
with open('user.pickle', 'rb') as f:
    set_pickle = pickle.load(f)
    print(type(set_pickle))
    print(set_pickle)
相關文章
相關標籤/搜索