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获取Linux系统的各种信息
Jul 10 Python
Python实现批量把SVG格式转成png、pdf格式的代码分享
Aug 21 Python
python实现的重启关机程序实例
Aug 21 Python
python中实现php的var_dump函数功能
Jan 21 Python
python使用cStringIO实现临时内存文件访问的方法
Mar 26 Python
详解Python中dict与set的使用
Aug 10 Python
Python数据结构之双向链表的定义与使用方法示例
Jan 16 Python
tensorflow实现简单的卷积神经网络
May 24 Python
python+pyqt5实现KFC点餐收银系统
Jan 24 Python
python字符串常用方法及文件简单读写的操作方法
Mar 04 Python
python数据分析工具之 matplotlib详解
Apr 09 Python
pyMySQL SQL语句传参问题,单个参数或多个参数说明
Jun 06 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
索尼ICF-SW100收音机评测
2021/03/02 无线电
通用PHP动态生成静态HTML网页的代码
2010/03/04 PHP
PHP获取当前页面完整URL的实现代码
2013/06/10 PHP
PHP中定义数组常量(array常量)的方法
2014/11/17 PHP
PHP+JS三级菜单联动菜单实现方法
2016/02/24 PHP
浅析PHP中的i++与++i的区别及效率
2016/06/15 PHP
php阳历转农历优化版
2016/08/08 PHP
php通过PHPExcel导入Excel表格到MySQL数据库的简单实例
2016/10/29 PHP
javascript iframe中打开文件,并检测iframe存在否
2008/12/28 Javascript
跟我一起学写jQuery插件开发方法(附完整实例及下载)
2010/04/01 Javascript
20款非常优秀的 jQuery 工具提示插件 推荐
2012/07/15 Javascript
javascript:void(0)的问题使用探讨
2014/04/10 Javascript
javascript数组去重的方法汇总
2015/04/14 Javascript
jQuery插件jRumble实现网页元素抖动
2015/06/05 Javascript
探讨JavaScript中的Rest参数和参数默认值
2015/07/29 Javascript
微信公众号-获取用户信息(网页授权获取)实现步骤
2016/10/21 Javascript
浅析如何利用angular结合translate为项目实现国际化
2016/12/08 Javascript
jQuery简单获取DIV和A标签元素位置的方法
2017/02/07 Javascript
jQuery给表格添加分页效果
2017/03/02 Javascript
浅谈vue中慎用style的scoped属性
2017/11/28 Javascript
VUE2.0 ElementUI2.0表格el-table自适应高度的实现方法
2018/11/28 Javascript
示例vue 的keep-alive缓存功能的实现
2018/12/13 Javascript
layui form表单提交之后重新加载数据表格的方法
2019/09/11 Javascript
Vue中key的作用示例代码详解
2020/06/10 Javascript
Vue实现todo应用的示例
2021/02/20 Vue.js
python 实现归并排序算法
2012/06/05 Python
python连接oracle数据库实例
2014/10/17 Python
Python的Urllib库的基本使用教程
2015/04/30 Python
python多线程socket编程之多客户端接入
2017/09/12 Python
对python中UDP,socket的使用详解
2019/08/22 Python
对python中的装包与解包实例详解
2019/08/24 Python
PySide2出现“ImportError: DLL load failed: 找不到指定的模块”的问题及解决方法
2020/06/10 Python
python中uuid模块实例浅析
2020/12/29 Python
HTML5 Canvas的性能提高技巧经验分享
2013/07/02 HTML / CSS
经理任命书模板
2014/06/06 职场文书
亮剑观后感600字
2015/06/05 职场文书