京公网安备 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
尊敬的考生: 您好! 我们诚挚通知您,CDA Level III 考试大纲将于 2025 年 12 月 31 日实施重大更新,并正式启用,2026年3月考 ...
2025-12-31“字如其人”的传统认知,让不少“手残党”在需要签名的场景中倍感尴尬——商务签约时的签名歪歪扭扭,朋友聚会的签名墙不敢落笔 ...
2025-12-31在多元统计分析的因子分析中,“得分系数”是连接原始观测指标与潜在因子的关键纽带,其核心作用是将多个相关性较高的原始指标, ...
2025-12-31对CDA(Certified Data Analyst)数据分析师而言,高质量的数据是开展后续分析、挖掘业务价值的基础,而数据采集作为数据链路的 ...
2025-12-31在中介效应分析(或路径分析)中,间接效应是衡量“自变量通过中介变量影响因变量”这一间接路径强度与方向的核心指标。不同于直 ...
2025-12-30数据透视表是数据分析中高效汇总、多维度分析数据的核心工具,能快速将杂乱数据转化为结构化的汇总报表。在实际分析场景中,我们 ...
2025-12-30在金融投资、商业运营、用户增长等数据密集型领域,量化策略凭借“数据驱动、逻辑可验证、执行标准化”的优势,成为企业提升决策 ...
2025-12-30CDA(Certified Data Analyst),是在数字经济大背景和人工智能时代趋势下,源自中国,走向世界,面向全行业的专业技能认证,旨 ...
2025-12-29在数据分析领域,周期性是时间序列数据的重要特征之一——它指数据在一定时间间隔内重复出现的规律,广泛存在于经济、金融、气象 ...
2025-12-29数据分析师的核心价值在于将海量数据转化为可落地的商业洞察,而高效的工具则是实现这一价值的关键载体。从数据采集、清洗整理, ...
2025-12-29在金融、零售、互联网等数据密集型行业,量化策略已成为企业提升决策效率、挖掘商业价值的核心工具。CDA(Certified Data Analys ...
2025-12-29CDA中国官网是全国统一的数据分析师认证报名网站,由认证考试委员会与持证人会员、企业会员以及行业知名第三方机构共同合作,致 ...
2025-12-26在数字化转型浪潮下,审计行业正经历从“传统手工审计”向“大数据智能审计”的深刻变革。教育部发布的《大数据与审计专业教学标 ...
2025-12-26统计学作为数学的重要分支,是连接数据与决策的桥梁。随着数据规模的爆炸式增长和复杂问题的涌现,传统统计方法已难以应对高维、 ...
2025-12-26数字化浪潮席卷全球,数据已成为企业核心生产要素,“用数据说话、用数据决策”成为企业生存与发展的核心逻辑。在这一背景下,CD ...
2025-12-26箱线图(Box Plot)作为数据分布可视化的核心工具,凭借简洁的结构直观呈现数据的中位数、四分位数、异常值等关键信息,广泛应用 ...
2025-12-25在数据驱动决策的时代,基于历史数据进行精准预测已成为企业核心需求——无论是预测未来销售额、客户流失概率,还是产品需求趋势 ...
2025-12-25在数据驱动业务的实践中,CDA(Certified Data Analyst)数据分析师的核心工作,本质上是通过“指标”这一数据语言,解读业务现 ...
2025-12-25在金融行业的数字化转型进程中,SQL作为数据处理与分析的核心工具,贯穿于零售银行、证券交易、保险理赔、支付结算等全业务链条 ...
2025-12-24在数据分析领域,假设检验是验证“数据差异是否显著”的核心工具,而独立样本t检验与卡方检验则是其中最常用的两种方法。很多初 ...
2025-12-24