首页 技术 正文
技术 2022年11月15日
0 收藏 978 点赞 3,562 浏览 1323 个字
#标准库地址:https://docs.python.org/2/library/marshal.html"""有时候,要把内存中一个对象持久化保存磁盘或者序列化二进制流通过网络发送到远程主机上,python有很多模块提供了序列化与反列化功能,如:marshal, pickle, cPickle等 注意: marshal并不是一个通用的模块,在某些时候它是一个不被推荐使用的模块,因为使用marshal序列化的二进制数据格式还没有文档化,在不同版本的Python中,marshal的实现可能不一样。也就是说,用python2.5序列为一个对象,用python2.6的程序反序列化所得到的对象,可能与原来的对象是不一样的。但这个模块存在的意义,正如Python手册中所说:The marshal module exists mainly to support reading and writing the “pseudo-compiled” code for Python modules of .pyc files."""import marshal#相关函数#marshal.dump(value,file[,version])"""将值写入到一个打开的输出流里。参数value表示待序列化的值。file表示打开的输出流。如:以”wb”模式打开的文件,sys.stdout或者os.popen。对于一些不支持序列类的类型,dump方法将抛出ValueError异常。要特别说明一下,并不是所有类型的对象都可以使用marshal模块来序列化/反序列化的。在python2.6中,支持的类型包括:None, integers, long integers, floating point numbers, strings, Unicode objects, tuple, list, set,dict, 和 code objects。对于tuple, list, set, dict等集合对象,其中的元素必须也是上述类型之一。"""#marshal.load(file)#它与marshal.dump相反操作,将二进制数据反序列为python对象,例如import sys,oslst=[1,(2,'string'),{'key':'value'}]#写入到文件中fle=open(os.path.join(os.getcwd(),'fle.txt'),'wb')marshal.dump(lst,fle)fle.close()#反序列化fle1=open(os.path.join(os.getcwd(),'fle.txt'),'rb')lst1=marshal.load(fle1)fle1.close()#打印print lstprint lst1#marshal.dumps(value[, version)#与marshal.dump()功能类似,它只返回序列化之后二进制流,#marsahl.load(string):将二进制流反序列化为对象。lst   =   [ 1 ,   ( 2 ,   " string " ) ,   { " key " :   " Value " } ]b1=marshal.dumps(lst)b2=marshal.loads(b1)print lst,b2
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,077
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,552
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,401
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,176
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,813
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,896