python使用marshal模塊序列化實例
來源:易賢網 閱讀:1294 次 日期:2014-09-26 08:32:20
溫馨提示:易賢網小編為您整理了“python使用marshal模塊序列化實例”,方便廣大網友查閱!

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

先來看看下面這段代碼:

?12345678910111213141516171819202122232425262728 import marshal data1 = ['abc',12,23,'jb51'] #幾個測試數據 data2 = {1:'aaa',"b":'dad'} data3 = (1,2,4) output_file = open("a.txt",'wb')#把這些數據序列化到文件中,注:文件必須以二進制模式打開 marshal.dump(data1,output_file) marshal.dump(data2,output_file) marshal.dump(data3,output_file) output_file.close() input_file = open('a.txt','rb')#從文件中讀取序列化的數據 #data1 = [] data1 = marshal.load(input_file) data2 = marshal.load(input_file) data3 = marshal.load(input_file) print data1#給同志們打印出結果看看 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

結果:

?1234 ['abc', 12, 23, 'jb51'] {1: 'aaa', 'b': 'dad'} (1, 2, 4) ['abc', 12, 23, 'jb51']

marshel模塊的幾個函數官方描述如下:

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.

更多信息請查看IT技術專欄

更多信息請查看腳本欄目
易賢網手機網站地址:python使用marshal模塊序列化實例

2025國考·省考課程試聽報名

  • 報班類型
  • 姓名
  • 手機號
  • 驗證碼
關于我們 | 聯系我們 | 人才招聘 | 網站聲明 | 網站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 新媒體/短視頻平臺 | 手機站點 | 投訴建議
工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網安備53010202001879號 人力資源服務許可證:(云)人服證字(2023)第0102001523號
聯系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關注公眾號:hfpxwx
咨詢QQ:1093837350(9:00—18:00)版權所有:易賢網