0
点赞
收藏
分享

微信扫一扫

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

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 条评论