python使用marshal模块序列化实例


Posted in Python onSeptember 25, 2014

本文实例讲述了python使用marshal模块序列化的方法,分享给大家供大家参考。具体方法如下:

先来看看下面这段代码:

import marshal
data1 = ['abc',12,23,'3water']  #几个测试数据
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

结果:

['abc', 12, 23, '3water']
{1: 'aaa', 'b': 'dad'}
(1, 2, 4)
['abc', 12, 23, '3water']

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版本之间的兼容性,所以保留个版本信息的函数.

希望本文所述对大家Python程序设计的学习有所帮助。

Python 相关文章推荐
简单介绍Python中用于求最小值的min()方法
May 15 Python
python实现简单socket通信的方法
Apr 19 Python
Python PyAutoGUI模块控制鼠标和键盘实现自动化任务详解
Sep 04 Python
Python实现的服务器示例小结【单进程、多进程、多线程、非阻塞式】
May 23 Python
使用python画社交网络图实例代码
Jul 10 Python
opencv之为图像添加边界的方法示例
Dec 26 Python
Python标准库itertools的使用方法
Jan 17 Python
简单了解django处理跨域请求最佳解决方案
Mar 25 Python
使用Jupyter notebooks上传文件夹或大量数据到服务器
Apr 14 Python
django inspectdb 操作已有数据库数据的使用步骤
Feb 07 Python
python模块内置属性概念及实例
Feb 18 Python
python中filter,map,reduce的作用
Jun 10 Python
python中类的一些方法分析
Sep 25 #Python
python实现获取序列中最小的几个元素
Sep 25 #Python
python中bisect模块用法实例
Sep 25 #Python
python实现给字典添加条目的方法
Sep 25 #Python
python实现忽略大小写对字符串列表排序的方法
Sep 25 #Python
python对字典进行排序实例
Sep 25 #Python
python实现在无须过多援引的情况下创建字典的方法
Sep 25 #Python
You might like
php数组函数序列之array_unshift() 在数组开头插入一个或多个元素
2011/11/07 PHP
laravel实现于语言包的完美切换方法
2019/09/29 PHP
laravel 中某一字段自增、自减的例子
2019/10/11 PHP
js实现页面跳转重定向的几种方式
2014/05/29 Javascript
js判断浏览器类型为ie6时不执行
2014/06/15 Javascript
浅谈javascript 迭代方法
2015/01/21 Javascript
JavaScript中Math.SQRT2属性的使用详解
2015/06/14 Javascript
javascript动态添加checkbox复选框的方法
2015/12/23 Javascript
js事件处理程序跨浏览器解决方案
2016/03/27 Javascript
jQuery绑定事件-多种实现方式总结
2016/05/09 Javascript
深入理解JavaScript中的尾调用(Tail Call)
2017/02/07 Javascript
node.js的事件机制
2017/02/08 Javascript
详解基于Angular4+ server render(服务端渲染)开发教程
2017/08/28 Javascript
JS使用正则表达式获取小括号、中括号及花括号内容的方法示例
2018/06/01 Javascript
使用JavaScript保存文本文件到本地的两种方法
2019/01/22 Javascript
浏览器事件循环与vue nextTicket的实现
2019/04/16 Javascript
vue滚动固定顶部及修改样式的实例代码
2019/05/30 Javascript
Anaconda 离线安装 python 包的操作方法
2018/06/11 Python
详解python3中的真值测试
2018/08/13 Python
在Python 字典中一键对应多个值的实例
2019/02/03 Python
5款Python程序员高频使用开发工具推荐
2019/04/10 Python
使用Python做定时任务及时了解互联网动态
2019/05/15 Python
Python企业编码生成系统之主程序模块设计详解
2019/07/26 Python
用python实现名片管理系统
2020/06/18 Python
python 爬虫网页登陆的简单实现
2020/11/30 Python
CSS3近阶段篇之酷炫的3D旋转透视
2016/04/28 HTML / CSS
CSS3 文字动画效果
2020/11/12 HTML / CSS
英国著名的茶叶品牌:Whittard of Chelsea
2016/09/22 全球购物
竞聘医务工作人员的自我评价分享
2013/11/04 职场文书
物业门卫岗位职责
2013/12/28 职场文书
写演讲稿所需要注意的4个条件
2014/01/09 职场文书
大学生职业规划论文
2014/01/11 职场文书
领导党性分析材料
2014/02/15 职场文书
设计师求职信
2014/07/01 职场文书
婚内分居协议书范文
2014/11/26 职场文书
2015年安全生产月工作总结
2015/07/27 职场文书