python使用marshal模塊序列化實(shí)例
本文實(shí)例講述了python使用marshal模塊序列化的方法,分享給大家供大家參考。具體方法如下:
先來(lái)看看下面這段代碼:
import marshal data1 = ['abc',12,23,'jb51'] #幾個(gè)測(cè)試數(shù)據(jù) data2 = {1:'aaa',"b":'dad'} data3 = (1,2,4) output_file = open("a.txt",'wb')#把這些數(shù)據(jù)序列化到文件中,注:文件必須以二進(jìn)制模式打開(kāi) marshal.dump(data1,output_file) marshal.dump(data2,output_file) marshal.dump(data3,output_file) output_file.close() input_file = open('a.txt','rb')#從文件中讀取序列化的數(shù)據(jù) #data1 = [] data1 = marshal.load(input_file) data2 = marshal.load(input_file) data3 = marshal.load(input_file) print data1#給同志們打印出結(jié)果看看 print data2 print data3 outstring = marshal.dumps(data1)#marshal.dumps()返回是一個(gè)字節(jié)串,該字節(jié)串用于寫(xiě)入文件 open('out.txt','wb').write(outstring) file_data = open('out.txt','rb').read() real_data = marshal.loads(file_data) print real_data
結(jié)果:
['abc', 12, 23, 'jb51'] {1: 'aaa', 'b': 'dad'} (1, 2, 4) ['abc', 12, 23, 'jb51']
marshel模塊的幾個(gè)函數(shù)官方描述如下:
The module defines these functions:
marshal.dump(value, file[, version])
Write the value on the open file. The value must be a supported type. The file must be an open file object such as sys.stdout or returned by open() or os.popen(). It must be opened in binary mode ('wb' or 'w+b').
If the value has (or contains an object that has) an unsupported type, a ValueError exception is raised — but garbage data will also be written to the file. The object will not be properly read back by load().
New in version 2.4: The version argument indicates the data format that dump should use (see below).
marshal.load(file)
Read one value from the open file and return it. If no valid value is read (e.g. because the data has a different Python version's incompatible marshal format), raise EOFError, ValueError or TypeError. The file must be an open file object opened in binary mode ('rb' or 'r+b').
Warning
If an object containing an unsupported type was marshalled with dump(), load() will substitute None for the unmarshallable type.
marshal.dumps(value[, version])
Return the string that would be written to a file by dump(value, file). The value must be a supported type. Raise a ValueError exception if value has (or contains an object that has) an unsupported type.
New in version 2.4: The version argument indicates the data format that dumps should use (see below).
marshal.loads(string)
Convert the string to a value. If no valid value is found, raise EOFError, ValueError or TypeError. Extra characters in the string are ignored.
In addition, the following constants are defined:
marshal.version
Indicates the format that the module uses.
marshal.version的用處:marshal不保證不同的python版本之間的兼容性,所以保留個(gè)版本信息的函數(shù).
希望本文所述對(duì)大家Python程序設(shè)計(jì)的學(xué)習(xí)有所幫助。
- 詳解Python中的序列化與反序列化的使用
- 淺析Python中的序列化存儲(chǔ)的方法
- Python 序列化 pickle/cPickle模塊使用介紹
- Python pickle類(lèi)庫(kù)介紹(對(duì)象序列化和反序列化)
- 詳解Python 序列化Serialize 和 反序列化Deserialize
- 老生常談Python序列化和反序列化
- python使用cPickle模塊序列化實(shí)例
- Python實(shí)現(xiàn)JSON反序列化類(lèi)對(duì)象的示例
- 詳解Python之?dāng)?shù)據(jù)序列化(json、pickle、shelve)
- 一篇文章了解Python中常見(jiàn)的序列化操作
相關(guān)文章
Python根據(jù)區(qū)號(hào)生成手機(jī)號(hào)碼的方法
這篇文章主要介紹了Python根據(jù)區(qū)號(hào)生成手機(jī)號(hào)碼的方法,涉及Python隨機(jī)數(shù)與字符串的相關(guān)操作技巧,需要的朋友可以參考下2015-07-07Python爬蟲(chóng)之UserAgent的使用實(shí)例
今天小編就為大家分享一篇關(guān)于Python爬蟲(chóng)之UserAgent的使用實(shí)例,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-02-02Python利用itchat對(duì)微信中好友數(shù)據(jù)實(shí)現(xiàn)簡(jiǎn)單分析的方法
Python 熱度一直很高,我感覺(jué)這就是得益于擁有大量的包資源,極大的方便了開(kāi)發(fā)人員的需求。下面這篇文章主要給大家介紹了關(guān)于Python利用itchat實(shí)現(xiàn)對(duì)微信中好友數(shù)據(jù)進(jìn)行簡(jiǎn)單分析的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下。2017-11-11Python中DataFrame判斷兩列數(shù)據(jù)是否相等的方法
本文主要介紹了DataFrame判斷兩列數(shù)據(jù)是否相等的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04TensorFlow2基本操作之 張量排序 填充與復(fù)制 查找與替換
這篇文章主要介紹了TensorFlow2基本操作之 張量排序 填充與復(fù)制 查找與替換,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-09-09使用python解析xml成對(duì)應(yīng)的html示例分享
這篇文章主要介紹了使用python解析xml成對(duì)應(yīng)的html示例,需要的朋友可以參考下2014-04-04淺析python中的絕對(duì)導(dǎo)入和相對(duì)導(dǎo)入
這篇文章主要是想和大家簡(jiǎn)單聊聊python中絕對(duì)導(dǎo)入和相對(duì)導(dǎo)入的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,需要的可以參考下2023-09-09