亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

python使用marshal模塊序列化實例

系統(tǒng) 2103 0

本文實例講述了python使用marshal模塊序列化的方法,分享給大家供大家參考。具體方法如下:

先來看看下面這段代碼:

            
import marshal
data1 = ['abc',12,23,'jb51']  #幾個測試數(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()返回是一個字節(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模塊的幾個函數(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版本之間的兼容性,所以保留個版本信息的函數(shù).

希望本文所述對大家Python程序設(shè)計的學(xué)習(xí)有所幫助。


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦?。。?/p>

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 在线观看三级拍拍视频 | 天天操婷婷 | 夜色福利久久久久久777777 | 在线观看欧美亚洲日本专区 | 国产精品区一区二区免费 | 久久9966精品国产免费 | 最新国产精品好看的国产精品 | 久久99精品国产 | 免费一级欧美毛片 | 在线视频久 | 天天做夜夜做久久做狠狠 | 91精品成人免费国产片 | 亚洲国产综合精品中文第一区 | 依人九九 | 亚洲欧美日韩不卡一区二区三区 | 欧美亚洲精品一区二三 | 一本伊在人香蕉线观新在线 | 免费观看h片 | 日韩视频 中文字幕 视频一区 | 亚洲精品久中文字幕 | 日日操夜夜操免费视频 | 欧美成人禁片在线观看网址 | 四虎永久免费884hutv | 97视频在线观看免费视频 | 99热这里只有精品7 99热这里只有精品8 | 亚洲 国产 日韩 欧美 | 曰本一级毛片免费播放 | 伊人中文字幕 | 日本老年人精品久久中文字幕 | xxxxxxx泡妞中国| 老司机午夜视频在线观看 | 九九精品视频在线播放 | 亚洲国产婷婷香蕉久久久久久 | 国产成人性毛片aaww | 日本欧美小视频 | 日日射视频 | 888奇米影视 | 九九资源站 | 日本一本一道久久香蕉免费 | 国产在线一区二区三区在线 | 精品久久久久久久久免费影院 |