中文字幕免费精品_亚洲视频自拍_亚洲综合国产激情另类一区_色综合咪咪久久

python使用marshal模塊序列化實(shí)例
來(lái)源:易賢網(wǎng) 閱讀:1017 次 日期:2016-06-22 09:23:29
溫馨提示:易賢網(wǎng)小編為您整理了“python使用marshal模塊序列化實(shí)例”,方便廣大網(wǎng)友查閱!

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

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

?12345678910111213141516171819202122232425262728 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é)果:

?1234 ['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.

更多信息請(qǐng)查看腳本欄目
易賢網(wǎng)手機(jī)網(wǎng)站地址:python使用marshal模塊序列化實(shí)例
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

2026國(guó)考·省考課程試聽(tīng)報(bào)名

  • 報(bào)班類型
  • 姓名
  • 手機(jī)號(hào)
  • 驗(yàn)證碼
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡(jiǎn)要咨詢 | 簡(jiǎn)要咨詢須知 | 新媒體/短視頻平臺(tái) | 手機(jī)站點(diǎn) | 投訴建議
工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
云南網(wǎng)警備案專用圖標(biāo)
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號(hào):hfpxwx
咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報(bào)警專用圖標(biāo)
主站蜘蛛池模板: 九台市| 徐州市| 宜章县| 改则县| 恩平市| 武城县| 新津县| 溧阳市| 息烽县| 平山县| 工布江达县| 榕江县| 肇源县| 都江堰市| 泸水县| 丰城市| 东乌| 怀集县| 虞城县| 商都县| 那坡县| 霍城县| 东乡县| 英山县| 呼伦贝尔市| 木里| 凤城市| 册亨县| 遂平县| 文化| 临汾市| 偏关县| 乌兰察布市| 韶关市| 耒阳市| 绥中县| 浦县| 称多县| 涟水县| 平谷区| 当涂县|