京公网安备 11010802034615号
经营许可证编号:京B2-20210330
python使用marshal模块序列化实例
本文实例讲述了python使用marshal模块序列化的方法,分享给大家供大家参考。具体方法如下:
先来看看下面这段代码:
import marshal
data1 = ['abc',12,23,'jb51'] #几个测试数据
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, 'jb51']
{1: 'aaa', 'b': 'dad'}
(1, 2, 4)
['abc', 12, 23, 'jb51']
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程序设计的学习有所帮助。
数据分析咨询请扫描二维码
若不方便扫码,搜微信号:CDAshujufenxi
手游行业具备用户迭代快、竞争激烈、用户粘性易流失的典型特征。随着新游持续上线、玩家审美升级、玩法疲劳等问题出现,存量用户 ...
2026-08-14在数字化产品运营、商业数据分析、业务增长管理中,零散的指标统计无法支撑系统性的业务决策。单一的点击率、转化率、销量数据只 ...
2026-08-14 很多数据分析师每天都在写SQL,但当被问到“数据查询语言(DQL)的本质是什么”“SELECT语句中各子句的书写顺序与实际执行顺 ...
2026-08-14在数据库数据分析、数据清洗、报表统计与业务查询场景中,日期时间是最高频、最核心的基础字段。数据库中存储的日期格式多样,包 ...
2026-08-13在数据统计分析与数据清洗工作中,箱线图是一种简洁高效、客观性强的数据可视化图表,能够直观呈现数据集的分布特征、离散程度和 ...
2026-08-13 很多数据分析师写过无数个SELECT查询,但当被问到“如何新建一张表来固化中间数据”“创建视图和创建物理表有什么区别”“视 ...
2026-08-13在自动化办公、数据采集、定时统计、日志清理、系统监控等场景中,程序往往需要按照固定时间间隔重复执行指定任务,这种运行机制 ...
2026-08-12在数据分析日常工作中,Excel数据筛选是数据清洗、数据提取、样本筛选的核心基础操作。传统Excel手动筛选、函数筛选方式,面对多 ...
2026-08-12 很多数据分析师精通Excel函数和数据透视表,但当被问到“数据从哪里来”“表和视图有什么区别”“数据库管理系统和SQL是什么 ...
2026-08-12在数据分析、统计建模、数据挖掘与商业调研过程中,原始数据往往无法做到绝对干净规整。受系统故障、人工录入失误、设备误差、突 ...
2026-08-11在数据分析工作中,聚类分析是典型的无监督学习方法,核心作用是依据数据自身的多维特征,将相似样本自动划分为若干类别,实现“ ...
2026-08-11 很多企业团队并非缺乏指标,而是陷入“指标失控”:仪表盘上堆满实时跳动的数据,却无法回答“当前瓶颈在哪、下一步该做什么 ...
2026-08-11AB实验是互联网产品迭代、营销优化、功能升级的核心科学验证手段,通过流量随机分组、对照组与实验组对比,科学验证策略、功能、 ...
2026-08-10在MySQL数据库优化中,索引是提升查询效率、降低数据库IO开销、优化系统性能的核心手段。普通单列索引仅适配简单查询场景,面对 ...
2026-08-10 很多数据分析师每天盯着几十个指标,但当被问到“这套指标要支撑什么业务目标”“指标之间是什么逻辑关系”“业务变化时如何 ...
2026-08-10在数字化市场调研体系中,大数据与小数据是两类核心调研数据形态,分别对应海量行为统计与精准样本深度调研。行业普遍存在认知误 ...
2026-08-07数据透视表是Excel、WPS中最核心的数据分析工具,凭借快速汇总、分组统计、动态筛选的优势,被广泛应用于销量统计、业绩复盘、数 ...
2026-08-07 很多数据分析师每天盯着GMV、DAU、转化率,但当被问到“哪些指标在所有行业都适用”“哪些指标只对电商有意义”“二者如何搭 ...
2026-08-07在商品销量、市场需求、营收规模等业务数据中,季节性波动是最普遍、最核心的数据特征。零售快消、食品餐饮、家电服饰、电商行业 ...
2026-08-06在流量红利消退、市场竞争白热化的商业环境中,传统依托经验、跟风投放、广撒网式的营销模式,逐渐暴露出成本高、精准度低、转化 ...
2026-08-06