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绘图库Matplotlib的安装
Jul 03 Python
用Python制作在地图上模拟瘟疫扩散的Gif图
Mar 31 Python
python抽象基类用法实例分析
Jun 04 Python
python实现list元素按关键字相加减的方法示例
Jun 09 Python
python3写的简单本地文件上传服务器实例
Jun 04 Python
浅谈Python反射 & 单例模式
Mar 21 Python
Python中Numpy ndarray的使用详解
May 24 Python
python实现二级登陆菜单及安装过程
Jun 21 Python
python 实现简单的FTP程序
Dec 27 Python
Python socket服务常用操作代码实例
Jun 22 Python
Python3自带工具2to3.py 转换 Python2.x 代码到Python3的操作
Mar 03 Python
OpenCV图像变换之傅里叶变换的一些应用
Jul 26 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
杏林同学录(三)
2006/10/09 PHP
phpStudy配置多站点多域名方法及遇到的403错误解决方法
2017/10/19 PHP
阿里云的WindowsServer2016上部署php+apache
2018/07/17 PHP
php对象工厂类完整示例
2018/08/09 PHP
如何取得中文输入的真实长度?
2006/06/24 Javascript
jQuery 页面 Mask实现代码
2010/01/09 Javascript
读jQuery之十二 删除事件核心方法
2011/07/31 Javascript
js 中的switch表达式使用示例
2020/06/03 Javascript
jQuery插件windowScroll实现单屏滚动特效
2015/07/14 Javascript
jQuery实现底部浮动窗口效果
2016/09/07 Javascript
浅谈js内置对象Math的属性和方法(推荐)
2016/09/19 Javascript
详解Node.Js如何处理post数据
2016/09/19 Javascript
如何使用angularJs
2017/05/08 Javascript
vue之nextTick全面解析
2017/05/17 Javascript
详解Angular2 关于*ngFor 嵌套循环
2017/05/22 Javascript
bootstrap轮播模板使用方法详解
2017/11/17 Javascript
Vue源码分析之Vue实例初始化详解
2019/08/25 Javascript
vue实现短信验证码登录功能(流程详解)
2019/12/10 Javascript
详解Vue3 Teleport 的实践及原理
2020/12/02 Vue.js
[51:29]完美世界DOTA2联赛循环赛 Matador vs Forest BO2第一场 11.05
2020/11/05 DOTA
Python和perl实现批量对目录下电子书文件重命名的代码分享
2014/11/21 Python
python中偏函数partial用法实例分析
2015/07/08 Python
如何用itertools解决无序排列组合的问题
2017/05/18 Python
python利用Tesseract识别验证码的方法示例
2019/01/21 Python
Django如何实现上传图片功能
2019/08/16 Python
基于Django框架的权限组件rbac实例讲解
2019/08/31 Python
django-crontab实现服务端的定时任务的示例代码
2020/02/17 Python
Python Handler处理器和自定义Opener原理详解
2020/03/05 Python
python中numpy数组与list相互转换实例方法
2021/01/29 Python
static全局变量与普通的全局变量有什么区别?static局部变量和普通局部变量有什么区别?static函数与普通函数有什么区别?
2015/02/22 面试题
医生实习工作总结的自我评价
2013/09/27 职场文书
物业客服专员岗位职责
2013/11/30 职场文书
建议书标准格式
2014/03/12 职场文书
承诺函范文
2015/01/21 职场文书
后天观后感
2015/06/08 职场文书
win10此电脑打不开怎么办 win10双击此电脑无响应的解决办法
2022/07/23 数码科技