麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > Python > 正文

python使用marshal模塊序列化實例

2020-02-23 05:53:14
字體:
來源:轉載
供稿:網友

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

先來看看下面這段代碼:

import marshaldata1 = ['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 data2print data3outstring = marshal.dumps(data1)#marshal.dumps()返回是一個字節串,該字節串用于寫入文件open('out.txt','wb').write(outstring)file_data = open('out.txt','rb').read()real_data = marshal.loads(file_data)print real_data

結果:

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

marshal.version的用處:marshal不保證不同的python版本之間的兼容性,所以保留個版本信息的函數.

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 成人在线不卡 | 亚洲成人自拍电影 | 日本羞羞影院 | 视频一区二区三区在线 | 亚洲一区中文字幕 | 国内精品久久久久影院不卡 | 国产精品久久久久久久久久大牛 | 久久久久国产精品久久久久 | 黄色电影免费网址 | 黄视频网站免费 | 亚洲精品成人在线视频 | 少妇的肉体的满足毛片 | 成年人福利视频 | 色戒在线版 | 亚洲一区 国产精品 | 成人在线影视 | 亚洲精品 在线播放 | 爱爱插插视频 | 成年人免费视频播放 | 成人免费在线网 | 亚洲欧洲日产v特级毛片 | 一级成人毛片 | 久久久精品视 | av色在线观看 | 中文字幕一二区 | 久久资源总站 | 亚洲最新黄色网址 | 日韩字幕 | 久久新地址 | 牛牛热这里只有精品 | 双性精h调教灌尿打屁股的文案 | 男女亲热网站 | 在线成人免费观看视频 | 国产精品自拍啪啪 | www.91视频com| 国产亚洲小视频 | 巨根插入 | 午夜视频在线 | h色网站在线观看 | 欧美在线观看视频一区二区 | 亚洲午夜影院在线观看 |