python使用marshal模块序列化实例,pythonmarshal


本文实例讲述了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程序设计的学习有所帮助。


怎定制Python模块的查找与物理实现

有时候会需要定制这个过程,比方说,嵌入Python作为 应用容器的时候,希望有一种特别的应用打包格式,类似jar或者war,或者处 于某些原因,需要改变Python Module的物理存储,比如,处于查找性能上的考 虑,如果python module能从一个key-value数据库得到就好了,或者处于商务 上的原因,如果能对源代码(或者pyc,而pyc很容易被反编译到py)进行加密 处理就更好了。
In [1]: import imp
In [3]: m = imp.new_module(test)
In [4]: code_obj = compile(import os, test.py, 'exec')
In [5]: code_obj
Out[5]: <code object <module at 0x31b5830, file test.py, line 1
In [6]: exec code_obj in m.__dict__In [7]: m
Out[7]: <module 'test' (built-in)
In [8]: dir(m)
Out[8]: ['__builtins__', '__doc__', '__name__', '__package__', 'os']
这里面,newmodule调用创建了一个相应的module对象,内置函数compile则从 一个字符串(源码)获得了相应的code object,该object可以被exec。
参考上面的链接。要点在于可以用一个实现了Import Protocol的class去 hook模块加载的过程,这个hook要被安装到sys.pathhooks.
这是一个从网络上(github)import模块的例子:
参考前面的代码,关键在于`exec source in m._dict_`这里,source可以 是一段源码,比如import os,也可以是一个code object,这个code object是可以被序列化和反序列化的,事实上,pyc就是该对象的序列化(加 上时间戳、magic number和crc校验),为了性能上的考虑,反序列化要比重 编译py源文件略好,可以把code object的序列化结果存储下来。在Importer 那里从该存储设备获取code object marshal string就行了。
•.从一个py文件获得其code object marshal string
import marshal
source = open(test.py).read()
co = compile(source, test.py, 'exec')
co_s = marshal.dumps(co)
•.从一个code object marshal string获得一个python module
import marshal,imp
def load_module(co_str):
 

C++怎解析Python用cPickle序列化到文件的数据?坐等大牛

百度看一看
 

评论关闭