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 内置字符串处理函数的使用方法
Jun 11 Python
Python的Flask框架中实现简单的登录功能的教程
Apr 20 Python
Python基于分水岭算法解决走迷宫游戏示例
Sep 26 Python
深入理解Python中的*重复运算符
Oct 28 Python
Numpy数组的保存与读取方法
Apr 04 Python
解决python大批量读写.doc文件的问题
May 08 Python
500行Python代码打造刷脸考勤系统
Jun 03 Python
PyQtGraph在pyqt中的应用及安装过程
Aug 04 Python
pytorch中tensor.expand()和tensor.expand_as()函数详解
Dec 27 Python
python基于Kivy写一个图形桌面时钟程序
Jan 28 Python
Python中的变量与常量
Nov 11 Python
Python pyecharts绘制条形图详解
Apr 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的库,结果发现很多东西
2006/12/31 PHP
php checkbox复选框值的获取与checkbox默认值输出方法
2010/05/15 PHP
使用PHP生成PDF方法详解
2015/01/23 PHP
详解Window7 下开发php扩展
2015/12/31 PHP
Yii框架使用魔术方法实现跨文件调用功能示例
2017/05/20 PHP
禁止直接访问php文件代码分享
2020/05/05 PHP
6个DIV 135或246间隔一秒轮番显示效果
2010/07/24 Javascript
nodejs开发环境配置与使用
2014/11/17 NodeJs
jQuery创建自定义的选择器用以选择高度大于100的超链接实例
2015/03/18 Javascript
js如何实现淡入淡出效果
2020/11/18 Javascript
基于JS如何实现类似QQ好友头像hover时显示资料卡的效果(推荐)
2016/06/09 Javascript
原生js获取浏览器窗口及元素宽高常用方法集合
2017/01/18 Javascript
MUI 上拉刷新/下拉加载功能实例代码
2017/04/13 Javascript
vue-cli中的webpack配置详解
2017/09/25 Javascript
element-ui和vue表单(对话框)验证提示语(残留)清除操作
2020/09/11 Javascript
Vue3 实现双盒子定位Overlay的示例
2020/12/22 Vue.js
[02:02:38]VG vs Mineski Supermajor 败者组 BO3 第一场 6.6
2018/06/07 DOTA
python根据经纬度计算距离示例
2014/02/16 Python
python实现udp数据报传输的方法
2014/09/26 Python
Python使用Shelve保存对象方法总结
2019/01/28 Python
Python3.4学习笔记之 idle 清屏扩展插件用法分析
2019/03/01 Python
记录Python脚本的运行日志的方法
2019/06/05 Python
tensorflow 获取checkpoint中的变量列表实例
2020/02/11 Python
在python中利用dict转json按输入顺序输出内容方式
2020/02/27 Python
HTML5 Canvas中绘制矩形实例
2015/01/01 HTML / CSS
html5唤起app的方法
2017/11/30 HTML / CSS
Debenhams爱尔兰:英国知名的百货公司
2017/01/02 全球购物
美国羊皮公司:Overland
2018/01/15 全球购物
Banana Republic英国官网:香蕉共和国,GAP集团旗下偏贵族风
2018/04/24 全球购物
奥地利度假券的专家:we-are.travel
2019/04/10 全球购物
八皇后问题,输出了所有情况,不过有些结果只是旋转了90度
2016/08/15 面试题
武汉英思工程科技有限公司–ORACLE面试测试题目
2012/04/30 面试题
社区庆八一活动方案
2014/02/02 职场文书
教室标语大全
2014/06/21 职场文书
医生学习党的群众路线教育实践活动心得体会
2014/11/03 职场文书
销售内勤岗位职责范本
2015/04/13 职场文书