python使用cPickle模块序列化实例
本文实例讲述了python使用cPickle模块序列化的方法,分享给大家供大家参考。
具体方法如下:
import cPickle data1 = ['abc',12,23] #几个测试数据 data2 = {1:'aaa',"b":'dad'} data3 = (1,2,4) output_file = open("a.txt",'w') cPickle.dump(data1,output_file) cPickle.dump(data2,output_file) cPickle.dump(data3,output_file) output_file.close() input_file = open('a.txt','rb') #data1 = [] data1 = cPickle.load(input_file) data2 = cPickle.load(input_file) data3 = cPickle.load(input_file) print data1 print data2 print data3 outstring = cPickle.dumps(data1) open('out.txt','wb').write(outstring) file_data = open('out.txt','rb').read() real_data = cPickle.loads(file_data) print real_data
本文实例测试环境Python2.7.6
运行结果如下:
['abc', 12L, 23L] {1L: 'aaa', 'b': 'dad'} (1L, 2L, 4L) ['abc', 12L, 23L]
希望本文所述对大家Python程序设计的学习有所帮助。
相关推荐
wenjs00 2020-05-29
longjing 2020-05-25
Jesus0 2010-06-15
82296530 2016-05-05
lfjjia 2011-07-25
快雪时晴天 2011-02-28
whynotgonow 2019-06-21
JayXio 2016-11-05
shipinsky 2013-01-12
faalr 2018-08-27
lxjzx0 2018-02-23
码猿同学 2019-02-28
Tonywei0 2015-01-03
mrwenhui 2012-07-30
cassiePython 2019-04-17
gglinux 2010-03-04
codemacket 2010-12-22
beatlinux 2009-10-21
python0 2014-01-03