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中的异常处理简明介绍
Apr 13 Python
matplotlib中legend位置调整解析
Dec 19 Python
Python实现文件信息进行合并实例代码
Jan 17 Python
查看django版本的方法分享
May 14 Python
python读写LMDB文件的方法
Jul 02 Python
Django Celery异步任务队列的实现
Jul 24 Python
Python while循环使用else语句代码实例
Feb 07 Python
浅谈pycharm导入pandas包遇到的问题及解决
Jun 01 Python
Python同时迭代多个序列的方法
Jul 28 Python
python中pdb模块实例用法
Jan 15 Python
selenium+超级鹰实现模拟登录12306
Jan 24 Python
Django项目在pycharm新建的步骤方法
Mar 02 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保留两位小数并且四舍五入及不四舍五入的方法
2013/09/22 PHP
PHP处理Oracle的CLOB实例
2014/11/03 PHP
PHP执行linux命令常用函数汇总
2016/02/02 PHP
php中让人头疼的浮点数运算分析
2016/10/10 PHP
PHP实现动态获取函数参数的方法示例
2018/04/02 PHP
Thinkphp5 如何隐藏入口文件index.php(URL重写)
2019/10/16 PHP
Laravel配合jwt使用的方法实例
2020/10/25 PHP
js或css实现滚动广告的几种方案
2010/01/28 Javascript
来自国外的14个图片放大编辑的jQuery插件整理
2010/10/20 Javascript
js获取select选中的option的text示例代码
2013/12/19 Javascript
node.js中的buffer.copy方法使用说明
2014/12/14 Javascript
Atitit.js的键盘按键事件捆绑and事件调度
2016/04/01 Javascript
jQuery中ScrollTo用法示例
2016/09/04 Javascript
详解Angular2中的编程对象Observable
2016/09/17 Javascript
jQuery Ajax 实现在html页面实时显示用户登录状态
2016/12/30 Javascript
原生js实现新闻列表展开/收起全文功能
2017/01/20 Javascript
基于jquery实现二级联动效果
2017/03/30 jQuery
基于JavaScript实现无缝滚动效果
2017/07/21 Javascript
解决layui 复选框等内置控件不显示的问题
2018/08/14 Javascript
vue实现动态显示与隐藏底部导航的方法分析
2019/02/11 Javascript
解决Vue+Electron下Vuex的Dispatch没有效果问题
2019/05/20 Javascript
基于JavaScript实现单例模式
2019/10/30 Javascript
python下读取公私钥做加解密实例详解
2017/03/29 Python
分析Python中解析构建数据知识
2018/01/20 Python
Python使用Pickle模块进行数据保存和读取的讲解
2019/04/09 Python
python实现飞机大战游戏
2020/10/26 Python
基于HTML5新特性Mutation Observer实现编辑器的撤销和回退操作
2016/01/11 HTML / CSS
台湾三立电视电商平台:电电购
2019/09/09 全球购物
ORACLE第二个十问
2013/12/14 面试题
几个Shell Script面试题
2012/08/31 面试题
市场部管理制度
2014/02/02 职场文书
就业意向书范本
2015/05/11 职场文书
有关西游记的读书笔记
2015/06/25 职场文书
2015年四年级班主任工作总结
2015/10/22 职场文书
学习心理学心得体会
2016/01/22 职场文书
导游词之安徽醉翁亭
2020/01/10 职场文书