python使用marshal模块序列化实例
来源: 阅读:810 次 日期:2016-06-22 09:23:29
温馨提示: 小编为您整理了“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()返回是一个字节串,该字节串用于写入文件 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.

更多信息请查看脚本栏目
由于各方面情况的不断调整与变化, 提供的所有考试信息和咨询回复仅供参考,敬请考生以权威部门公布的正式信息和咨询为准!

2025国考·省考课程试听报名

  • 报班类型
  • 姓名
  • 手机号
  • 验证码
关于我们 | 联系我们 | 人才招聘 | 网站声明 | 网站帮助 | 非正式的简要咨询 | 简要咨询须知 | 加入群交流 | 手机站点 | 投诉建议
工业和信息化部备案号:滇ICP备2023014141号-1 云南省教育厅备案号:云教ICP备0901021 滇公网安备53010202001879号 人力资源服务许可证:(云)人服证字(2023)第0102001523号
云南网警备案专用图标
联系电话:0871-65317125(9:00—18:00) 获取招聘考试信息及咨询关注公众号:
咨询QQ:526150442(9:00—18:00)版权所有:
云南网警报警专用图标
Baidu
map