本文实例讲述了python使用marshal模块序列化的方法,分享给大家供大家参考。具体方法如下:

先来看看下面这段代码:

?12345678910111213141516171819202122232425262728 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

结果:

?1234 ['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.

python marshal loads failed_python使用marshal模块序列化实例相关推荐

  1. python marshal loads failed_Python模块学习:marshal 对象的序列化

    有时候,要把内存中的一个对象持久化保存到磁盘上,或者序列化成二进制流通过网络发送到远程主机上.Python中有很多模块提供了序列化与反序列化的功能,如:marshal, pickle, cPickle ...

  2. python marshal loads failed_python – Marshal反序列化 – 不安全

    我在一个项目中工作,我使用cPickle快速加载文件.几天前我读到,元帅甚至可能比cPickle更快.它对我有用,但我很好奇,the documentation的这个警告是关于: Warning Th ...

  3. python marshal loads failed_在Python中marshal对象序列化的相关知识

    有时候,要把内存中的一个对象持久化保存到磁盘上,或者序列化成二进制流通过网络发送到远程主机上.Python中有很多模块提供了序列化与反序列化的功能,如:marshal, pickle, cPickle ...

  4. Python标准库中的marshal模块

     marshal-内部的Python对象序列化 该模块包含可以以二进制格式读取和写入Python值的函数.该格式是针对Python的,但独立于机器架构问题(例如,您可以将Python值写入PC上的 ...

  5. python内置方法+数学模块+随机模块+序列化模块

    内置方法 abs 绝对值函数 round 四舍五入 (n.5 n为偶数则舍去 n.5 n为奇数,则进一!) 特点:奇进偶不进 res = round(4.51) #正常的四舍五入 5 res = ro ...

  6. 十年测试经验的阿里p10讲解python初阶:函数和模块 python全栈自动化测试系类4-2

    还是老规矩,大家可能对凡哥不是很了解这里先和大家来个自我介绍 凡哥我已经有着十二年互联网自动化测试和测试开发工程师,拥有丰富的自动化测试平台及测试开发经验,擅长接口测试.Python自动化全栈,测试开 ...

  7. Python标准库中的pickle模块

     pickle  -  Python对象序列化. pickle模块实现了用于序列化和反序列化Python对象结构的二进制协议."pickle"是将Python对象层次结构转换为 ...

  8. 4-20模块 序列化模块 hashlib模块

    1,模块,py文件就是模块,py之所以好用就是模块多. 2,模块的分类: 1,内置模块,python 安装时自带的模块 2,扩展模块,别人写好的,需要安装之后,可以直接使用.itchat微信模块, b ...

  9. 使用pickle模块序列化数据,优化代码

    使用pickle模块序列化数据,优化代码 pickle是Python标准库中的一个二进制序列化和反序列化库. 可以以二进制的形式将数据持久化保存到磁盘文件中.可以将数据和代码分离,提高代码可读性和优雅 ...

最新文章

  1. 读懂这篇文章就懂大数据,3000字概括《大数据时代》
  2. SQL中declare申明变量
  3. 优化器(AdaGrad,AdaDelta,RmsProp,Adam,Nadam,Nesterovs,Sgd,momentum)
  4. nasm汇编:段的申明、$$、$
  5. Oracle之垂直水平分库分表(二)
  6. 深入分析驴子系列(2)
  7. 波形捕捉:(8)使用“捕捉缓冲区”
  8. java上帝模块常见的情况_JVM上帝视角看JVM内存模型,分而治之论各模块详情详解...
  9. 【参数辨识】六关节机械臂动力学参数辨识(上)
  10. 没想到Sharding-Jdbc竟然这么牛逼!一键实现读写分离、分库分表~
  11. [Python从零到壹] 七.网络爬虫之Requests爬取豆瓣电影TOP250及CSV存储
  12. julia Pkg.add() 安装package时卡着不动慢怎么办
  13. 安装vum、npm、cnpm、vue/cli
  14. 中国最牛天使投资人:30万元投资,回报超50000倍
  15. 微信小程序——获取步数
  16. RSS精选(一)-文摘
  17. 剑灵力士卡刀ahk_[游戏]剑灵剑士火系卡刀
  18. 源码之 LifeCycleOwner
  19. Cynthia问题、任务、缺陷管理系统
  20. 【转】电路板绘制经验积累 (一至五)

热门文章

  1. 一直在坚持一直在摸索
  2. 计算机音乐谱despacito,西班牙神曲《despacito》手风琴谱
  3. C#学习笔记:控件BackColor属性与ForeColor的使用方法
  4. 找回PPT中丢失的源文件数据
  5. C#中PictureBox异步加载图片
  6. 二、大端模式和小端模式
  7. ideal 首次使用无法导入jar包问题
  8. 【集思广益】关于手机3D APP
  9. 七夕情人节表白代码合集---爱她就要跟她说【含源码免费获取】
  10. 魅蓝note6屏幕总成更换操作