上代码:

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模块序列化相关推荐

  1. python marshal loads failed_python使用marshal模块序列化实例

    本文实例讲述了python使用marshal模块序列化的方法,分享给大家供大家参考.具体方法如下: 先来看看下面这段代码: ?1234567891011121314151617181920212223 ...

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

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

  3. Python标准库中的marshal模块

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

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

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

  5. Python使用marshal模块操作二进制文件

    Python标准库marshal可以进行对象的序列化和反序列化. >>> import marshal # 待序列化的对象 >>> x1 = 30 >> ...

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

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

  7. python中常用的序列化模块_第六章 常用模块(5):python常用模块(序列化模块:pickle,json,shelve,xml)...

    6.3.7 序列化模块 (pickle,json,shelve,xml) 文件写入,数据传输时,我们都是以字符串形式写入的(因为字符串可以encode成bytes). 那其他类型(比如字典,列表等)想 ...

  8. Python_异常和模块

    异常处理 单个异常处理: try:print(num)except NameError:print('没有定义变量')except FileNotFoundError:print('找不到文件路径') ...

  9. os模块,序列化模块,json模块,pickle模块

    OS模块 把路径中不符合规范的/改成操作系统默认的格式 1 path = os.path.abspath("D:\dayday\day 19\3.py") 2 print(path ...

  10. json/pickle模块(序列化)

    什么叫序列化? 序列化是指把内存里的数据类型转变成字符串,以使其能存储到硬盘或通过网络传输到远程,因为硬盘或网络传输时只能接受bytes 为什么要序列化? 你打游戏过程中,打累了,停下来,关掉游戏.想 ...

最新文章

  1. yolov5检测完不显示框和标注
  2. java程序员修炼之道 pdf_?活动丨和大咖云风来场1对1交流,分享《程序员修炼之道》心得...
  3. VTK:网格之SimpleElevationFilter
  4. mysql数据库的后_MySQL数据库误删后的回复技巧
  5. apt ubuntu 指定ipv4_如何使用 apt 命令安装软件
  6. 无法安装64位版本的office,因为在您的PC上找到了以下32位程序:microsoft visio professional 2013
  7. TensorFlow零基础入门指南——计算模型、数据模型、运行模型!
  8. 360互联网训练营第十四期——大数据技术开放日
  9. Maven常见警告解决办法
  10. python是最美的语言阅读答案_《拯救世界上最美的语言》阅读答案
  11. window.location.href跳转无效 IE Bug【转载】
  12. 怎么把程序内部坐标转为屏幕坐标,如何将工作空间坐标转换为屏幕坐标?
  13. Oracle数据库锁表查询
  14. [单片机] KeilC51单块数码管演示与原理
  15. english need study and insist 1
  16. 点击或长按复制打开微信H5落地页如何制作?
  17. CentOS安装Nginx 以及日志管理
  18. 云-移动手机端项目总结
  19. cocos如何在游戏中动态设置横竖屏
  20. 《创业大赛》—微电影影评

热门文章

  1. android 分享到YouTube失败 403
  2. 如何在html中做超链接,如何在HTML上做一个超链接?
  3. 信用报告中的个人基本信息如何更新?
  4. 【ES6】三种暴露方法和引入方式
  5. java医疗报销_医疗保险报销流程图(修改后)
  6. Navicat注释乱码
  7. 巨杉TechDay回顾 | 技术人的夏天 · 就是这么燃!
  8. 给想去阿里面试的同学一些意见
  9. 作业1:小型考勤登记表
  10. 信息收集(一)之whois查询