python序列化

import pickle
d=dict(url='index.html',title='首頁',content='內容')
byte_data=pickle.dumps(d)
print(byte_data)
with open(r'abc.pkl','wb') as filer:
    pickle.dump(d,filer)
filer.close()
with open(r'abc.pkl','rb') as f:
    aa=pickle.load(f)
    print(aa)
    print(type(aa))
import pickle
class Person:
    def __init__(self,name):
        self.name=name
    def show(self):
        print(self.name)

a=Person('123')
# 序列化對象
with open(r'abc.pkl','wb') as f:
    pickle.dump(a,f)
f.close()
# 反序列化
with open(r'abc.pkl','rb') as f:
    aa=pickle.load(f)
    print(aa.show())
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章