#!/usr/bin/python
#coding=utf-8
import os
import marshal,cPickle
"""
marshal只能序列化有限的类型
而cPickle能够序列化自定义的类型
"""
class Foo:
def __init__(self, name):
self.name = name
def __str__(self):
return self.name
o=range(0,10)
L = Foo("Goodspeed")
cls = [marshal,cPickle]
for c in cls:
print c.__name__
#序列化到文件中
fi =open(os.path.join(os.getcwd(),'fle.txt'),'wb')
c.dump(o,fi)
fi.close()
fi =open(os.path.join(os.getcwd(),'fle.txt'),'rb')
o1 = c.load(fi)
fi.close()
#序列化成字符串
d = c.dumps(o)
o2 = c.loads(d)
print o
print o1
print o2