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求列表交集的方法汇总
Nov 10 Python
python创建一个最简单http webserver服务器的方法
May 08 Python
python中string模块各属性以及函数的用法介绍
May 30 Python
python实现多线程的方式及多条命令并发执行
Jun 07 Python
python 遍历字符串(含汉字)实例详解
Apr 04 Python
Python中将变量按行写入txt文本中的方法
Apr 03 Python
python实现QQ邮箱/163邮箱的邮件发送
Jan 22 Python
Python给图像添加噪声具体操作
Mar 03 Python
python从list列表中选出一个数和其对应的坐标方法
Jul 20 Python
Python list与NumPy array 区分详解
Nov 06 Python
python实现与redis交互操作详解
Apr 21 Python
Python几种酷炫的进度条的方式
Apr 11 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开发大型项目的一点经验
2006/10/09 PHP
PHP实现微信发红包程序
2015/08/24 PHP
搭建自己的PHP MVC框架详解
2017/08/16 PHP
jQuery的三种$()
2009/12/30 Javascript
JavaScript中window、doucment、body的解释
2013/08/14 Javascript
基于MVC3方式实现下拉列表联动(JQuery)
2013/09/02 Javascript
jQuery extend 的简单实例
2013/09/18 Javascript
如何让你的Lightbox支持滚轮缩放及Base64图片
2014/12/04 Javascript
jQuery实现自动调整字体大小的方法
2015/06/15 Javascript
js实现文字在按钮上滚动的方法
2015/08/20 Javascript
基于javascript实现简单的抽奖系统
2020/04/15 Javascript
jQuery实现侧浮窗与中浮窗切换效果的方法
2016/09/05 Javascript
Bootstrap响应式侧边栏改进版
2016/09/17 Javascript
angularjs利用directive实现移动端自定义软键盘的示例
2017/09/20 Javascript
JS中使用cavas截图网页并解决跨域及模糊问题
2018/11/13 Javascript
vue鼠标悬停事件实例详解
2019/04/01 Javascript
vue请求本地自己编写的json文件的方法
2019/04/25 Javascript
Vue实现商品分类菜单数量提示功能
2019/07/26 Javascript
javascript的hashCode函数实现代码小结
2020/08/11 Javascript
浅谈vue项目,访问路径#号的问题
2020/08/14 Javascript
Jquery+javascript实现支付网页数字键盘
2020/12/21 jQuery
关于Python的一些学习总结
2018/05/25 Python
python实现剪切功能
2019/01/23 Python
Python Opencv图像处理基本操作代码详解
2020/08/31 Python
新加坡网上美容店:Hermo新加坡
2019/06/19 全球购物
迪士尼西班牙官方网上商店:ShopDisney西班牙
2020/02/02 全球购物
心得体会开头
2014/01/01 职场文书
实习生的自我评价
2014/01/08 职场文书
条幅标语大全
2014/06/20 职场文书
医药销售自我评价200字
2014/09/11 职场文书
乐山大佛导游词
2015/02/02 职场文书
《梅花魂》教学反思
2016/02/18 职场文书
导游词之平津战役纪念馆
2019/11/04 职场文书
悬疑名作《朋友游戏》动画无字ED宣传片 新角色公开
2022/04/13 日漫
Java工作中实用的代码优化技巧分享
2022/04/21 Java/Android
Win Server2016远程桌面如何允许多用户同时登录
2022/06/10 Servers