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 相关文章推荐
DJANGO-ALLAUTH社交用户系统的安装配置
Nov 18 Python
Python中基本的日期时间处理的学习教程
Oct 16 Python
Python使用add_subplot与subplot画子图操作示例
Jun 01 Python
对Tensorflow中权值和feature map的可视化详解
Jun 14 Python
python 寻找离散序列极值点的方法
Jul 10 Python
python 使用装饰器并记录log的示例代码
Jul 12 Python
Django基础知识 URL路由系统详解
Jul 18 Python
对django中foreignkey的简单使用详解
Jul 28 Python
python修改FTP服务器上的文件名
Sep 11 Python
Python实现把类当做字典来访问
Dec 16 Python
tensorflow 动态获取 BatchSzie 的大小实例
Jun 30 Python
Vs Code中8个好用的python 扩展插件
Oct 12 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 foreach正序倒序输出示例代码
2014/07/01 PHP
Zend Framework教程之路由功能Zend_Controller_Router详解
2016/03/07 PHP
Symfony2学习笔记之系统路由详解
2016/03/17 PHP
PHP实现的登录页面信息提示功能示例
2017/07/24 PHP
Firefox 无法获取cssRules 的解决办法
2006/10/11 Javascript
用JavaScript脚本实现Web页面信息交互
2006/12/21 Javascript
JavaScript 字符串处理函数使用小结
2010/12/02 Javascript
juqery 学习之六 CSS--css、位置、宽高
2011/02/11 Javascript
分享一个自定义的console类 让你不再纠结JS中的调试代码的兼容
2012/04/20 Javascript
javascript获取下拉列表框当中的文本值示例代码
2013/07/31 Javascript
离开当前页面前使用js判断条件提示是否要离开页面
2014/05/02 Javascript
nodejs的10个性能优化技巧
2014/07/15 NodeJs
用循环或if语句从json中取数据示例
2014/08/18 Javascript
jquery可定制的在线UEditor编辑器
2015/11/17 Javascript
jquery实现简单文字提示效果
2015/12/02 Javascript
JS读取XML文件数据并以table形式显示数据的方法(兼容IE与火狐)
2016/06/02 Javascript
[原创]JavaScript语法高亮插件highlight.js用法详解【附highlight.js本站下载】
2016/11/01 Javascript
Angular2 之 路由与导航详细介绍
2017/05/26 Javascript
前端常见跨域解决方案(全)
2017/09/19 Javascript
vue编译打包本地查看index文件的方法
2018/02/23 Javascript
vue双向数据绑定知识点总结
2018/04/18 Javascript
微信JSSDK实现打开摄像头拍照再将相片保存到服务器
2019/11/15 Javascript
vue中提示$index is not defined错误的解决方式
2020/09/02 Javascript
python-opencv在有噪音的情况下提取图像的轮廓实例
2017/08/30 Python
用Python实现大文本文件切割的方法
2019/01/12 Python
Python: glob匹配文件的操作
2020/12/11 Python
巴西女装购物网站:Eclectic
2018/04/24 全球购物
eBay比利时购物网站:eBay.be
2019/08/09 全球购物
UNIX命令速查表
2012/03/10 面试题
迎新晚会主持词
2014/03/24 职场文书
我的老师教学反思
2014/05/01 职场文书
金融保险专业求职信
2014/09/03 职场文书
个人授权委托书范文
2014/09/21 职场文书
庆七一宣传标语
2014/10/08 职场文书
三峡导游词
2015/01/31 职场文书
导游词之南迦巴瓦峰
2019/11/19 职场文书