本文實(shí)例講述了python使用cpickle模塊序列化的方法,分享給大家供大家參考。
具體方法如下:
?123456789101112131415161718192021222324252627282930 import cpickle data1 = ['abc',12,23] #幾個(gè)測(cè)試數(shù)據(jù) 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
本文實(shí)例測(cè)試環(huán)境python2.7.6
運(yùn)行結(jié)果如下:
?1234 ['abc', 12l, 23l] {1l: 'aaa', 'b': 'dad'} (1l, 2l, 4l) ['abc', 12l, 23l]
希望本文所述對(duì)大家python程序設(shè)計(jì)的學(xué)習(xí)有所幫助。