最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python使用marshal模塊序列化實(shí)例

 更新時(shí)間:2014年09月25日 16:46:05   投稿:shichen2014  
這篇文章主要介紹了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)制模式打開
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é)串用于寫入文件
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í)有所幫助。

相關(guān)文章

  • django安裝xadmin及問(wèn)題解決

    django安裝xadmin及問(wèn)題解決

    本文主要介紹了django安裝xadmin及問(wèn)題解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • pycharm遠(yuǎn)程連接服務(wù)器并配置python interpreter的方法

    pycharm遠(yuǎn)程連接服務(wù)器并配置python interpreter的方法

    這篇文章主要介紹了pycharm遠(yuǎn)程連接服務(wù)器并配置python interpreter的方法,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-12-12
  • python 判斷一組數(shù)據(jù)是否符合正態(tài)分布

    python 判斷一組數(shù)據(jù)是否符合正態(tài)分布

    這篇文章主要介紹了python 如何判斷一組數(shù)據(jù)是否符合正態(tài)分布,幫助大家更好的利用python分析數(shù)據(jù),感興趣的朋友可以了解下
    2020-09-09
  • python轉(zhuǎn)化excel數(shù)字日期為標(biāo)準(zhǔn)日期操作

    python轉(zhuǎn)化excel數(shù)字日期為標(biāo)準(zhǔn)日期操作

    這篇文章主要介紹了python轉(zhuǎn)化excel數(shù)字日期為標(biāo)準(zhǔn)日期操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-07-07
  • python通過(guò)安裝itchat包實(shí)現(xiàn)微信自動(dòng)回復(fù)收到的春節(jié)祝福

    python通過(guò)安裝itchat包實(shí)現(xiàn)微信自動(dòng)回復(fù)收到的春節(jié)祝福

    這篇文章主要介紹了python通過(guò)安裝itchat包實(shí)現(xiàn)微信自動(dòng)回復(fù)收到的春節(jié)祝福,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2020-01-01
  • Django框架安裝及項(xiàng)目創(chuàng)建過(guò)程解析

    Django框架安裝及項(xiàng)目創(chuàng)建過(guò)程解析

    這篇文章主要介紹了Django框架安裝及項(xiàng)目創(chuàng)建過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-09-09
  • python win32 簡(jiǎn)單操作方法

    python win32 簡(jiǎn)單操作方法

    下面小編就為大家?guī)?lái)一篇python win32 簡(jiǎn)單操作方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-05-05
  • matplotlib繪制兩點(diǎn)間連線的幾種方法實(shí)現(xiàn)

    matplotlib繪制兩點(diǎn)間連線的幾種方法實(shí)現(xiàn)

    本文主要介紹了matplotlib繪制兩點(diǎn)間連線的幾種方法實(shí)現(xiàn),主要介紹了4種方法,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • 基于python 二維數(shù)組及畫圖的實(shí)例詳解

    基于python 二維數(shù)組及畫圖的實(shí)例詳解

    下面小編就為大家分享一篇基于python 二維數(shù)組及畫圖的實(shí)例詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-04-04
  • python 獲取字符串MD5值方法

    python 獲取字符串MD5值方法

    今天小編就為大家分享一篇python 獲取字符串MD5值方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-05-05

最新評(píng)論

伊川县| 永胜县| 郁南县| 边坝县| 张家港市| 贵港市| 东海县| 利川市| 通州区| 承德县| 浦城县| 昭通市| 宣化县| 漳浦县| 八宿县| 故城县| 西乌珠穆沁旗| 广宁县| 错那县| 普安县| 绍兴县| 靖边县| 玛纳斯县| 贵溪市| 乌兰察布市| 馆陶县| 土默特右旗| 平顶山市| 湟中县| 中超| 天气| 奇台县| 达尔| 晴隆县| 阿尔山市| 阿克| 日喀则市| 江陵县| 白河县| 塔河县| 日喀则市|