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脚本生成sitemap.xml的实现方法
Jan 31 Python
Unicode和Python的中文处理
Mar 19 Python
使用k8s部署Django项目的方法步骤
Jan 14 Python
解决python线程卡死的问题
Feb 18 Python
对PyQt5基本窗口控件 QMainWindow的使用详解
Jun 19 Python
pandas的连接函数concat()函数的具体使用方法
Jul 09 Python
python实现车牌识别的示例代码
Aug 05 Python
python中struct模块之字节型数据的处理方法
Aug 27 Python
Python matplotlib画图时图例说明(legend)放到图像外侧详解
May 16 Python
手把手教你从PyCharm安装到激活(最新激活码),亲测有效可激活至2089年
Nov 25 Python
彻底解决pip下载pytorch慢的问题方法
Mar 01 Python
python小程序之飘落的银杏
Apr 17 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/07/08 PHP
PHP define函数的使用说明
2008/08/27 PHP
PHP实现下载功能的代码
2012/09/29 PHP
php生成固定长度纯数字编码的方法
2015/07/09 PHP
如何通过Linux命令行使用和运行PHP脚本
2015/07/29 PHP
javascript返回顶部效果(自写代码)
2013/01/06 Javascript
关闭ie窗口清除Session的解决方法
2014/01/10 Javascript
jquery简单实现幻灯片的方法
2015/08/03 Javascript
JavaScript文本框脚本编写的注意事项
2016/01/25 Javascript
JavaScript模拟鼠标右键菜单效果
2020/12/08 Javascript
微信小程序侧边栏滑动特效(左右滑动)
2017/01/23 Javascript
详解webpack 配合babel 将es6转成es5 超简单实例
2017/05/02 Javascript
nodejs 简单实现动态html的方法
2018/05/12 NodeJs
js+html5实现手机九宫格密码解锁功能
2018/07/30 Javascript
如何使用pm2快速将项目部署到远程服务器
2019/03/12 Javascript
JS中注入eval, Function等系统函数截获动态代码
2019/04/03 Javascript
nodejs实现用户登录路由功能
2019/05/22 NodeJs
Vue项目总结之webpack常规打包优化方案
2019/06/06 Javascript
linux服务器快速卸载安装node环境(简单上手)
2021/02/22 Javascript
[04:48]DOTA2上海特锦赛小组赛第三日 TOP10精彩集锦
2016/02/28 DOTA
Python实现的百度站长自动URL提交小工具
2014/06/27 Python
Django中几种重定向方法
2015/04/28 Python
python常用知识梳理(必看篇)
2017/03/23 Python
Python简单实现socket信息发送与监听功能示例
2018/01/03 Python
Python文件路径名的操作方法
2019/10/30 Python
Matplotlib 绘制饼图解决文字重叠的方法
2020/07/24 Python
捷克汽车配件和工具销售网站:TorriaCars
2018/02/26 全球购物
Nicole Miller官方网站:纽约女装品牌
2019/09/14 全球购物
Laravel中Kafka的使用详解
2021/03/24 PHP
六一儿童节活动策划方案
2014/01/27 职场文书
HR求职自荐信范文
2014/06/21 职场文书
旅游饭店管理专业自荐书
2014/06/28 职场文书
甲午大海战观后感
2015/06/02 职场文书
优秀范文:读《红岩》有感3篇
2019/10/14 职场文书
导游词之日月潭
2019/11/05 职场文书
MySQL详细讲解变量variables的用法
2022/06/21 MySQL