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 01 Python
用Python编写一个简单的Lisp解释器的教程
Apr 03 Python
Python打造出适合自己的定制化Eclipse IDE
Mar 02 Python
Django自定义插件实现网站登录验证码功能
Apr 19 Python
Python3中使用PyMongo的方法详解
Jul 28 Python
Python中static相关知识小结
Jan 02 Python
python之pyqt5通过按钮改变Label的背景颜色方法
Jun 13 Python
python实现自动化上线脚本的示例
Jul 01 Python
pandas分区间,算频率的实例
Jul 04 Python
Python 实现训练集、测试集随机划分
Jan 08 Python
python 正则表达式参数替换实例详解
Jan 17 Python
django教程如何自学
Jul 31 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部分常见问题总结
2008/03/27 PHP
如何使用纯PHP实现定时器任务(Timer)
2015/07/31 PHP
PHP开发中AJAX技术的简单应用
2015/12/11 PHP
PHP 接入支付宝即时到账功能
2016/09/18 PHP
PHP中OpenSSL加密问题整理
2017/12/14 PHP
PHP的mysqli_set_charset()函数讲解
2019/01/23 PHP
JavaScript 学习笔记(十六) js事件
2010/02/01 Javascript
JavaScript的parseInt 取整使用
2011/05/09 Javascript
jsvascript图像处理—(计算机视觉应用)图像金字塔
2013/01/15 Javascript
深入理解javaScript中的事件驱动
2013/05/21 Javascript
js自动查找select下拉的菜单并选择(示例代码)
2014/02/26 Javascript
innerHTML动态添加html代码和脚本兼容多个浏览器
2014/10/11 Javascript
js实现按钮加背景图片常用方法
2014/11/01 Javascript
javascript中checkbox使用方法实例演示
2015/11/19 Javascript
详谈JavaScript的闭包及应用
2017/01/17 Javascript
js遮罩效果制作弹出注册界面效果
2017/01/25 Javascript
微信小程序登录换取token的教程
2018/05/31 Javascript
Javascript中弹窗confirm与prompt的区别
2018/10/26 Javascript
vue-cli配置全局sass、less变量的方法
2019/06/06 Javascript
vue组件传值的实现方式小结【三种方式】
2020/02/05 Javascript
[34:44]Liquid vs TNC Supermajor 胜者组 BO3 第二场 6.4
2018/06/05 DOTA
python二叉树的实现实例
2013/11/21 Python
Python 字符串大小写转换的简单实例
2017/01/21 Python
python实现Floyd算法
2018/01/03 Python
Python使用cx_Freeze库生成msi格式安装文件的方法
2018/07/10 Python
Python面向对象基础入门之设置对象属性
2018/12/11 Python
一个可以套路别人的python小程序实例代码
2019/04/09 Python
Django使用模板后无法找到静态资源文件问题解决
2019/07/19 Python
Python IDE Pycharm中的快捷键列表用法
2019/08/08 Python
学习python需要有编程基础吗
2020/06/02 Python
关于Python错误重试方法总结
2021/01/03 Python
Big Green Smile法国:领先的英国有机和天然产品在线商店
2021/01/02 全球购物
营销总经理的岗位职责
2013/12/15 职场文书
新学期感想
2015/08/10 职场文书
Html5页面播放M4a音频文件
2021/03/30 HTML / CSS
Mysql中调试存储过程最简单的方法
2021/06/30 MySQL