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 相关文章推荐
Python3 socket同步通信简单示例
Jun 07 Python
Python文件操作之合并文本文件内容示例代码
Sep 19 Python
Django 2.0版本的新特性抢先看!
Jan 05 Python
Python3多线程操作简单示例
May 22 Python
python opencv 二值化 计算白色像素点的实例
Jul 03 Python
python3.7简单的爬虫实例详解
Jul 08 Python
Python2与Python3的区别点整理
Dec 12 Python
vue学习笔记之动态组件和v-once指令简单示例
Feb 29 Python
python检查目录文件权限并修改目录文件权限的操作
Mar 11 Python
Pycharm中安装wordcloud等库失败问题及终端通过pip安装的Python库如何添加到Pycharm解释器中(推荐)
May 10 Python
Python基础进阶之海量表情包多线程爬虫功能的实现
Dec 17 Python
全网最细 Python 格式化输出用法讲解(推荐)
Jan 18 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
10个实用的PHP代码片段
2011/09/02 PHP
Session的工作机制详解和安全性问题(PHP实例讲解)
2014/04/10 PHP
php函数与传递参数实例分析
2014/11/15 PHP
PHP模板解析类实例
2015/07/09 PHP
PHP实现动态web服务器方法
2015/07/29 PHP
Laravel 队列使用的实现
2019/01/08 PHP
TP5框架安全机制实例分析
2020/04/05 PHP
PHP替换Word中变量并导出PDF图片的实现方法
2020/11/26 PHP
jquery 简短右键菜单 多浏览器兼容
2010/01/01 Javascript
js操作table示例(个人心得)
2013/11/29 Javascript
JQuery实现防止退格键返回的方法
2015/02/12 Javascript
jQuery中选择器的基础使用教程
2016/05/23 Javascript
jQuery.parseHTML() 函数详解
2017/01/09 Javascript
JS栈stack类的实现与使用方法示例
2019/01/31 Javascript
学习RxJS之JavaScript框架Cycle.js
2019/06/17 Javascript
Vue数据绑定实例写法
2019/08/06 Javascript
Vue实现页面添加水印功能
2019/11/09 Javascript
js观察者模式的弹幕案例
2020/11/23 Javascript
Python实现竖排打印传单手机号码易撕条
2015/03/16 Python
Python 装饰器实现DRY(不重复代码)原则
2018/03/05 Python
python2.7实现爬虫网页数据
2018/05/25 Python
Django 浅谈根据配置生成SQL语句的问题
2018/05/29 Python
Python获取二维数组的行列数的2种方法
2020/02/11 Python
Python如何安装第三方模块
2020/05/28 Python
Python绘图实现台风路径可视化代码实例
2020/10/23 Python
基于python实现坦克大战游戏
2020/10/27 Python
使用HTML5 Geolocation实现一个距离追踪器
2018/04/09 HTML / CSS
微软新西兰官方网站:Microsoft New Zealand
2018/08/17 全球购物
编码转换,怎样实现将GB2312编码的字符串转换为ISO-8859-1编码的字符串
2014/01/07 面试题
企业法人代表证明书
2014/09/27 职场文书
2014年体育教学工作总结
2014/12/09 职场文书
学校党员干部承诺书
2015/05/04 职场文书
学雷锋献爱心活动总结
2015/05/11 职场文书
利用Matlab绘制各类特殊图形的实例代码
2021/07/16 Python
以下牛机,你有几个
2022/04/05 无线电
MySQL中dd::columns表结构转table过程及应用详解
2022/09/23 MySQL