python_使用marshal模块序列化
上代码:
import marshal
data1 = ['abc',12,23] #几个测试数据
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 data3outstring = 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]
{1: 'aaa', 'b': 'dad'}
(1, 2, 4)
['abc', 12, 23]
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_使用marshal模块序列化相关推荐
- python marshal loads failed_python使用marshal模块序列化实例
本文实例讲述了python使用marshal模块序列化的方法,分享给大家供大家参考.具体方法如下: 先来看看下面这段代码: ?1234567891011121314151617181920212223 ...
- python marshal loads failed_在Python中marshal对象序列化的相关知识
有时候,要把内存中的一个对象持久化保存到磁盘上,或者序列化成二进制流通过网络发送到远程主机上.Python中有很多模块提供了序列化与反序列化的功能,如:marshal, pickle, cPickle ...
- Python标准库中的marshal模块
marshal-内部的Python对象序列化 该模块包含可以以二进制格式读取和写入Python值的函数.该格式是针对Python的,但独立于机器架构问题(例如,您可以将Python值写入PC上的 ...
- 使用pickle模块序列化数据,优化代码
使用pickle模块序列化数据,优化代码 pickle是Python标准库中的一个二进制序列化和反序列化库. 可以以二进制的形式将数据持久化保存到磁盘文件中.可以将数据和代码分离,提高代码可读性和优雅 ...
- Python使用marshal模块操作二进制文件
Python标准库marshal可以进行对象的序列化和反序列化. >>> import marshal # 待序列化的对象 >>> x1 = 30 >> ...
- 4-20模块 序列化模块 hashlib模块
1,模块,py文件就是模块,py之所以好用就是模块多. 2,模块的分类: 1,内置模块,python 安装时自带的模块 2,扩展模块,别人写好的,需要安装之后,可以直接使用.itchat微信模块, b ...
- python中常用的序列化模块_第六章 常用模块(5):python常用模块(序列化模块:pickle,json,shelve,xml)...
6.3.7 序列化模块 (pickle,json,shelve,xml) 文件写入,数据传输时,我们都是以字符串形式写入的(因为字符串可以encode成bytes). 那其他类型(比如字典,列表等)想 ...
- Python_异常和模块
异常处理 单个异常处理: try:print(num)except NameError:print('没有定义变量')except FileNotFoundError:print('找不到文件路径') ...
- os模块,序列化模块,json模块,pickle模块
OS模块 把路径中不符合规范的/改成操作系统默认的格式 1 path = os.path.abspath("D:\dayday\day 19\3.py") 2 print(path ...
- json/pickle模块(序列化)
什么叫序列化? 序列化是指把内存里的数据类型转变成字符串,以使其能存储到硬盘或通过网络传输到远程,因为硬盘或网络传输时只能接受bytes 为什么要序列化? 你打游戏过程中,打累了,停下来,关掉游戏.想 ...
最新文章
- yolov5检测完不显示框和标注
- java程序员修炼之道 pdf_?活动丨和大咖云风来场1对1交流,分享《程序员修炼之道》心得...
- VTK:网格之SimpleElevationFilter
- mysql数据库的后_MySQL数据库误删后的回复技巧
- apt ubuntu 指定ipv4_如何使用 apt 命令安装软件
- 无法安装64位版本的office,因为在您的PC上找到了以下32位程序:microsoft visio professional 2013
- TensorFlow零基础入门指南——计算模型、数据模型、运行模型!
- 360互联网训练营第十四期——大数据技术开放日
- Maven常见警告解决办法
- python是最美的语言阅读答案_《拯救世界上最美的语言》阅读答案
- window.location.href跳转无效 IE Bug【转载】
- 怎么把程序内部坐标转为屏幕坐标,如何将工作空间坐标转换为屏幕坐标?
- Oracle数据库锁表查询
- [单片机] KeilC51单块数码管演示与原理
- english need study and insist 1
- 点击或长按复制打开微信H5落地页如何制作?
- CentOS安装Nginx 以及日志管理
- 云-移动手机端项目总结
- cocos如何在游戏中动态设置横竖屏
- 《创业大赛》—微电影影评