国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > Python > 正文

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

2019-11-25 18:11:51
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

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

先來(lái)看看下面這段代碼:

import marshaldata1 = ['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 data2print data3outstring = 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í)有所幫助。

發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 缙云县| 铜鼓县| 吉林省| 宁南县| 利津县| 宣化县| 平罗县| 德化县| 台东市| 永修县| 庆元县| 东丽区| 津南区| 宝坻区| 东兴市| 玛曲县| 嘉峪关市| 澄城县| 富平县| 二连浩特市| 大足县| 怀化市| 固阳县| 潮安县| 岢岚县| 资中县| 六枝特区| 长岭县| 日喀则市| 镇雄县| 游戏| 安仁县| 胶南市| 广饶县| 奈曼旗| 荥经县| 甘肃省| 鹤岗市| 万源市| 林甸县| 巍山|