Python pickle類庫介紹(對象序列化和反序列化)
一、pickle
pickle模塊用來實現(xiàn)python對象的序列化和反序列化。通常地pickle將python對象序列化為二進(jìn)制流或文件。
python對象與文件之間的序列化和反序列化:
pickle.dump()
pickle.load()
如果要實現(xiàn)python對象和字符串間的序列化和反序列化,則使用:
pickle.dumps()
pickle.loads()
可以被序列化的類型有:
* None,True 和 False;
* 整數(shù),浮點數(shù),復(fù)數(shù);
* 字符串,字節(jié)流,字節(jié)數(shù)組;
* 包含可pickle對象的tuples,lists,sets和dictionaries;
* 定義在module頂層的函數(shù):
* 定義在module頂層的內(nèi)置函數(shù);
* 定義在module頂層的類;
* 擁有__dict__()或__setstate__()的自定義類型;
注意:對于函數(shù)或類的序列化是以名字來識別的,所以需要import相應(yīng)的module。
二、pickle的運(yùn)行過程
在大部分情況下,要是的對象picklable,我們不需要額外的代碼。默認(rèn)地pickle將智能地檢查類和實例的屬性,當(dāng)一個類實例反序列化的時候,它的__init__()方法通常不被調(diào)用。而是首先創(chuàng)建一個未初始化的實例,然后再回復(fù)存儲的屬性。
但是可以通過實現(xiàn)下列的方法來修改默認(rèn)的行為:
object.__getstate__() :默認(rèn)地序列化對象的__dict__,但是如果你實現(xiàn)了__getstate__(),則__getstate__()函數(shù)返回的值將被序列化。
object.__setstate__(state) :如果類型實現(xiàn)了此方法,則在反序列化的時候,此方法用來恢復(fù)對象的屬性。
object.__getnewargs__() : 如果實例構(gòu)造的時候(__new__())需要參數(shù),則需要實現(xiàn)此函數(shù)。
注意:如果__getstate__()返回False,則在反序列化的時候__setstate__()則不被調(diào)用。
有的時候為了效率,或上面的3個函數(shù)不能滿足需求時,需要實現(xiàn)__reduce__()函數(shù)。
三、實例
import pickle
# An arbitrary collection of objects supported by pickle.
data = {
'a': [1, 2.0, 3, 4+6j],
'b': ("character string", b"byte string"),
'c': set([None, True, False])
}
with open('data.pickle', 'wb') as f:
# Pickle the 'data' dictionary using the highest protocol available.
pickle.dump(data, f, pickle.HIGHEST_PROTOCOL)
with open('data.pickle', 'rb') as f:
# The protocol version used is detected automatically, so we do not
# have to specify it.
data = pickle.load(f)
print(str(data))
四、修改picklable類型的默認(rèn)行為
class TextReader:
"""Print and number lines in a text file."""
def __init__(self, filename):
self.filename = filename
self.file = open(filename)
self.lineno = 0
def readline(self):
self.lineno += 1
line = self.file.readline()
if not line:
return None
if line.endswith('\n'):
line = line[:-1]
return "%i: %s" % (self.lineno, line)
def __getstate__(self):
# Copy the object's state from self.__dict__ which contains
# all our instance attributes. Always use the dict.copy()
# method to avoid modifying the original state.
state = self.__dict__.copy()
# Remove the unpicklable entries.
del state['file']
return state
def __setstate__(self, state):
# Restore instance attributes (i.e., filename and lineno).
self.__dict__.update(state)
# Restore the previously opened file's state. To do so, we need to
# reopen it and read from it until the line count is restored.
file = open(self.filename)
for _ in range(self.lineno):
file.readline()
# Finally, save the file.
self.file = file
reader = TextReader("hello.txt")
print(reader.readline())
print(reader.readline())
s = pickle.dumps(reader)
#print(s)
new_reader = pickle.loads(s)
print(new_reader.readline())
# the output is
# 1: hello
# 2: how are you
# 3: goodbye
相關(guān)文章
在Pycharm中設(shè)置默認(rèn)自動換行的方法
今天小編就為大家分享一篇在Pycharm中設(shè)置默認(rèn)自動換行的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-01-01Python3使用matplotlib繪圖時,坐標(biāo)軸刻度不從X軸、y軸兩端開始
這篇文章主要介紹了Python3使用matplotlib繪圖時,坐標(biāo)軸刻度不從X軸、y軸兩端開始問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08Python高級排序sort()函數(shù)使用技巧實例探索
本文詳細(xì)介紹sort()函數(shù)的使用,包括基本排序、自定義排序、逆序排序等多種情況,并提供大量示例代碼,以幫助你充分理解和掌握這一函數(shù)的用法,探索更多sort()排序函數(shù)的作用2024-01-01Python使用os.listdir()和os.walk()獲取文件路徑與文件下所有目錄的方法
今天小編就為大家分享一篇關(guān)于Python使用os.listdir()和os.walk()獲取文件路徑與文件下所有目錄的方法,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-04-04Python自動調(diào)用IE打開某個網(wǎng)站的方法
這篇文章主要介紹了Python自動調(diào)用IE打開某個網(wǎng)站的方法,涉及Python調(diào)用系統(tǒng)win32組件的相關(guān)技巧,需要的朋友可以參考下2015-06-06python GUI庫圖形界面開發(fā)之PyQt5 Qt Designer工具(Qt設(shè)計師)詳細(xì)使用方法及Designer
這篇文章主要介紹了python GUI庫圖形界面開發(fā)之PyQt5 Qt Designer工具(Qt設(shè)計師)詳細(xì)使用方法及Designer ui文件轉(zhuǎn)py文件方法,需要的朋友可以參考下2020-02-02淺談opencv自動光學(xué)檢測、目標(biāo)分割和檢測(連通區(qū)域和findContours)
這篇文章主要介紹了淺談opencv自動光學(xué)檢測、目標(biāo)分割和檢測(連通區(qū)域和findContours),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06