利用 pickle 模块序列化与反序列化python对象——简单读/写

阅读 105

2022-02-06

Pickle:

import pickle

def wirtePickleTxt(txt_path, txt):
    with open(txt_path, 'wb') as f:
        pickle.dump(txt_path, txt)

def loadPickleTxt(txt_path):
    with open(txt_path, 'rb') as f:
        txt = pickle.load(f)
    return txt

将序列化后对象保存到‘TXT.pkl’中

txt = "Hi, I am Dawn. What's your name?"
wirtePickleTxt('./TXT.pkl', txt)
txt = loadPickleTxt('./TXT.pkl')
print(txt)
txt = {'d1':[1,2,3,4,4,4,3,2,5],'d2':[6,6,7,0,0,2,1,1,1,3]}
wirtePickleTxt('./TXT.pkl', txt)
txt = loadPickleTxt('./TXT.pkl')
print(txt)

精彩评论(0)

0 0 举报