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抓取网页内容示例分享
Feb 24 Python
python opencv之分水岭算法示例
Feb 24 Python
numpy matrix和array的乘和加实例
Jun 28 Python
python实现linux下抓包并存库功能
Jul 18 Python
总结Python图形用户界面和游戏开发知识点
May 22 Python
Python3安装psycopy2以及遇到问题解决方法
Jul 03 Python
python 寻找离散序列极值点的方法
Jul 10 Python
Python unittest单元测试框架及断言方法
Apr 15 Python
python导入库的具体方法
Jun 18 Python
Windows下pycharm安装第三方库失败(通用解决方案)
Sep 17 Python
如何利用python 读取配置文件
Jan 06 Python
基于PyTorch实现一个简单的CNN图像分类器
May 29 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 self,$this,const,static,->的使用
2009/10/22 PHP
PHP里的中文变量说明
2011/07/23 PHP
php获取文件大小的方法
2014/02/26 PHP
php递归调用删除数组空值元素的方法
2015/04/28 PHP
jQuery实现冻结表头的方法
2015/03/09 Javascript
jQuery实现checkbox全选的方法
2015/06/10 Javascript
JS实现的文字与图片定时切换效果代码
2015/10/06 Javascript
基于JS如何实现给字符加千分符(65,541,694,158)
2016/08/03 Javascript
js 获取html5的data属性实现方法
2017/07/28 Javascript
原生JS实现小小的音乐播放器
2017/10/16 Javascript
基于JavaScript实现表格滚动分页
2017/11/22 Javascript
javascript按顺序加载运行js方法
2017/12/01 Javascript
利用ES6实现单例模式及其应用详解
2017/12/09 Javascript
React中的refs的使用教程
2018/02/13 Javascript
jQuery实现左右滑动的toggle方法
2018/03/03 jQuery
vue.js实现格式化时间并每秒更新显示功能示例
2018/07/07 Javascript
使用JS判断页面是首次被加载还是刷新
2019/05/26 Javascript
vue请求数据的三种方式
2020/03/04 Javascript
解决VUE自定义拖拽指令时 onmouseup 与 click事件冲突问题
2020/07/24 Javascript
Python+Django在windows下的开发环境配置图解
2009/11/11 Python
python连接mysql调用存储过程示例
2014/03/05 Python
Python装饰器用法示例小结
2018/02/11 Python
Python随机函数random()使用方法小结
2018/04/29 Python
Sanic框架路由用法实例分析
2018/07/16 Python
使用python根据端口号关闭进程的方法
2018/11/06 Python
详解django+django-celery+celery的整合实战
2019/03/19 Python
JAVA语言如何进行异常处理,关键字:throws,throw,try,catch,finally分别代表什么意义?在try块中可以抛出异常吗?
2013/07/02 面试题
会计与出纳自荐书范文
2014/03/16 职场文书
股权转让协议书范本
2014/04/12 职场文书
村庄绿化方案
2014/05/07 职场文书
简单租房协议书范本
2014/08/20 职场文书
四风问题查摆材料
2014/08/25 职场文书
处级领导干部四风问题自我剖析材料
2014/09/29 职场文书
平安建设汇报材料
2014/12/29 职场文书
MySQL查询日期时间
2022/05/15 MySQL
微前端qiankun改造日渐庞大的项目教程
2022/06/21 Javascript