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的*args和**kwargs用法
Nov 01 Python
python基础教程之实现石头剪刀布游戏示例
Feb 11 Python
python对json的相关操作实例详解
Jan 04 Python
Python 含参构造函数实例详解
May 25 Python
Python实现图片拼接的代码
Jul 02 Python
详解pandas安装若干异常及解决方案总结
Jan 10 Python
使用python PIL库实现简单验证码的去噪方法步骤
May 10 Python
pycharm双击无响应(打不开问题解决办法)
Jan 10 Python
python+opencv边缘提取与各函数参数解析
Mar 09 Python
python 使用cx-freeze打包程序的实现
Mar 14 Python
python3中数组逆序输出方法
Dec 01 Python
基于tensorflow权重文件的解读
May 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
mysql_num_rows VS COUNT 效率问题分析
2011/04/23 PHP
浅谈ThinkPHP的URL重写
2014/11/25 PHP
js导入导出excel(实例代码)
2013/11/25 Javascript
Javascript浅谈之this
2013/12/17 Javascript
js实现字符串的16进制编码不加密
2014/04/25 Javascript
Node.js插件的正确编写方式
2014/08/03 Javascript
jquery 插件实现多行文本框[textarea]自动高度
2015/03/04 Javascript
cookie的secure属性详解
2015/04/08 Javascript
tuzhu_req.js 实现仿百度图片首页效果
2015/08/11 Javascript
JavaScript实现99乘法表及隔行变色实例代码
2016/02/24 Javascript
JavaScript数据存储 Cookie篇
2016/07/02 Javascript
关于动态执行代码(js的Eval)实例详解
2016/08/15 Javascript
js实现省份下拉菜单效果
2017/02/15 Javascript
手把手搭建安装基于windows的Vue.js运行环境
2017/06/12 Javascript
three.js实现3D视野缩放效果
2017/11/16 Javascript
解决vue 格式化银行卡(信用卡)每4位一个符号隔断的问题
2018/09/14 Javascript
使用Node.js写一个代码生成器的方法步骤
2019/05/10 Javascript
小程序中canvas的drawImage方法参数使用详解
2019/07/04 Javascript
小程序实现投票进度条
2019/11/20 Javascript
在vue中配置不同的代理同时访问不同的后台操作
2020/09/11 Javascript
[42:32]DOTA2上海特级锦标赛B组资格赛#2 Fnatic VS Spirit第二局
2016/02/27 DOTA
[01:05:41]EG vs Optic Supermajor 败者组 BO3 第二场 6.6
2018/06/07 DOTA
python 控制语句
2011/11/03 Python
使用scrapy实现爬网站例子和实现网络爬虫(蜘蛛)的步骤
2014/01/23 Python
Python中atexit模块的基本使用示例
2015/07/08 Python
python中reload(module)的用法示例详解
2017/09/15 Python
使用TensorFlow实现SVM
2018/09/06 Python
pycharm 解除默认unittest模式的方法
2018/11/30 Python
详解Python数据分析--Pandas知识点
2019/03/23 Python
使用Django简单编写一个XSS平台的方法步骤
2019/03/25 Python
浅谈django2.0 ForeignKey参数的变化
2019/08/06 Python
python 利用jinja2模板生成html代码实例
2019/10/10 Python
四年大学生活的个人自我评价
2013/12/11 职场文书
财务统计员岗位职责
2015/04/14 职场文书
会计专业自荐信范文
2019/05/22 职场文书
Mysql实现简易版搜索引擎的示例代码
2021/08/30 MySQL