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正则匹配抓取豆瓣电影链接和评论代码分享
Dec 27 Python
Python使用正则表达式抓取网页图片的方法示例
Apr 21 Python
python 二分查找和快速排序实例详解
Oct 13 Python
Python实现按照指定要求逆序输出一个数字的方法
Apr 19 Python
对numpy数据写入文件的方法讲解
Jul 09 Python
对pandas通过索引提取dataframe的行方法详解
Feb 01 Python
使用Python制作一个打字训练小工具
Oct 01 Python
Python迭代器模块itertools使用原理解析
Dec 11 Python
Python ORM编程基础示例
Feb 02 Python
TensorFlow的环境配置与安装方法
Feb 20 Python
python自动统计zabbix系统监控覆盖率的示例代码
Apr 03 Python
pytorch 预训练模型读取修改相关参数的填坑问题
Jun 05 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生成自己的LOG文件
2006/10/09 PHP
php 处理上百万条的数据库如何提高处理查询速度
2010/02/08 PHP
深入apache配置文件httpd.conf的部分参数说明
2013/06/28 PHP
对联广告js flash激活
2006/10/19 Javascript
前淘宝前端开发工程师阿当的PPT中有JS技术理念问题
2010/01/15 Javascript
JS实现常见的TAB、弹出层效果(TAB标签,斑马线,遮罩层等)
2015/10/08 Javascript
使用pcs api往免费的百度网盘上传下载文件的方法
2016/03/17 Javascript
Javascript对象字面量的理解
2016/06/22 Javascript
浅谈js对象属性 通过点(.) 和方括号([]) 的不同之处
2016/10/29 Javascript
Vue.js组件tabs实现选项卡切换效果
2016/12/01 Javascript
JavaScript实现元素滚动条到达一定位置循环追加内容
2017/12/28 Javascript
12条写出高质量JS代码的方法
2018/01/07 Javascript
Vue组件和Route的生命周期实例详解
2018/02/10 Javascript
使用typescript开发angular模块并发布npm包
2018/04/19 Javascript
小程序登录/注册页面设计的实现代码
2019/05/24 Javascript
使用vue3重构拼图游戏的实现示例
2021/01/25 Vue.js
Python3.X 线程中信号量的使用方法示例
2017/07/24 Python
python实现redis三种cas事务操作
2017/12/19 Python
python微信跳一跳游戏辅助代码解析
2018/01/29 Python
python 限制函数调用次数的实例讲解
2018/04/21 Python
python+selenium打印当前页面的titl和url方法
2018/06/22 Python
ActiveMQ:使用Python访问ActiveMQ的方法
2019/01/30 Python
django 环境变量配置过程详解
2019/08/06 Python
基于Python+Appium实现京东双十一自动领金币功能
2019/10/31 Python
python GUI库图形界面开发之PyQt5信号与槽基础使用方法与实例
2020/03/06 Python
python字典和json.dumps()的遇到的坑分析
2020/03/11 Python
python 实现图像快速替换某种颜色
2020/06/04 Python
HTML5 Blob对象的具体使用
2020/05/22 HTML / CSS
在html页面中取得session中的值的方法
2020/08/11 HTML / CSS
FORZIERI福喜利中国官网:奢侈品购物梦工厂
2019/05/03 全球购物
惠普新加坡官方商店:HP Singapore
2020/04/17 全球购物
横幅标语大全
2014/06/17 职场文书
优秀共产党员事迹材料
2014/12/18 职场文书
2015年教师节慰问信
2015/03/23 职场文书
java基础——多线程
2021/07/03 Java/Android
SpringCloud超详细讲解Feign声明式服务调用
2022/06/21 Java/Android