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 相关文章推荐
scrapy爬虫完整实例
Jan 25 Python
python 巧用正则寻找字符串中的特定字符的位置方法
May 02 Python
python读写LMDB文件的方法
Jul 02 Python
Python面向对象程序设计之继承与多继承用法分析
Jul 13 Python
CentOS7下python3.7.0安装教程
Jul 30 Python
django进阶之cookie和session的使用示例
Aug 17 Python
python匹配两个短语之间的字符实例
Dec 25 Python
详解Numpy数组转置的三种方法T、transpose、swapaxes
May 27 Python
在python plt图表中文字大小调节的方法
Jul 08 Python
Python HTMLTestRunner可视化报告实现过程解析
Apr 10 Python
OpenCV+Python3.5 简易手势识别的实现
Dec 21 Python
python 实现图片裁剪小工具
Feb 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/10/09 PHP
Linux下PHP连接Oracle数据库
2014/08/20 PHP
PHP入门教程之数学运算技巧总结
2016/09/11 PHP
Tinymce+jQuery.Validation使用产生的BUG
2010/03/29 Javascript
分享2个jQuery插件--jquery.fileupload与artdialog
2014/12/26 Javascript
jquery实现简单实用的弹出层效果代码
2015/10/15 Javascript
浅析如何利用JavaScript进行语音识别
2016/10/27 Javascript
js实现手机发送验证码功能
2017/03/13 Javascript
微信小程序实现轮播图效果
2017/09/07 Javascript
详解自定义ajax支持跨域组件封装
2018/02/08 Javascript
node.js基础知识小结
2018/02/26 Javascript
快速解决vue动态绑定多个class的官方实例语法无效的问题
2018/09/05 Javascript
nodejs aes 加解密实例
2018/10/10 NodeJs
el-input 标签中密码的显示和隐藏功能的实例代码
2019/07/19 Javascript
用Angular实现一个扫雷的游戏示例
2020/05/15 Javascript
详解Django通用视图中的函数包装
2015/07/21 Python
Python使用中文正则表达式匹配指定中文字符串的方法示例
2017/01/20 Python
基于Python实现扑克牌面试题
2019/12/11 Python
Anaconda使用IDLE的实现示例
2020/09/23 Python
python 利用toapi库自动生成api
2020/10/19 Python
HTML5 visibilityState属性详细介绍和使用实例
2014/05/03 HTML / CSS
Jogun Shop中文官网:韩国知名时尚男装网站
2016/10/12 全球购物
Snapfish英国:在线照片打印和个性化照片礼品
2017/01/13 全球购物
西班牙太阳镜品牌:Hawkers
2018/03/11 全球购物
Lookfantastic俄罗斯:欧洲在线化妆品零售商
2019/08/06 全球购物
企业行政文员岗位职责
2013/12/03 职场文书
简历的个人自我评价范文
2014/01/03 职场文书
腾讯广告词
2014/03/19 职场文书
四群教育工作实施方案
2014/03/26 职场文书
诚信承诺书模板
2014/05/26 职场文书
党员三严三实心得体会
2014/10/13 职场文书
2014年语文教师工作总结
2014/12/18 职场文书
2015年中学校长工作总结
2015/05/19 职场文书
2016年五一劳动节专题校园广播稿
2015/12/17 职场文书
2016三八妇女节校园广播稿
2015/12/17 职场文书
MySQL数据库 任意ip连接方法
2022/05/20 MySQL