ちょっとした設定や計算の途中結果を一時保存するには便利な環境。
import os,marshal
def writedump():
data1 = range(1,100,2)
data2 = ['hori','suzuki','yamada']
with open('datafile.dat','wb') as outfile:
marshal.dump(data1,outfile)
marshal.dump(data2,outfile)
def readdump():
with open('datafile.dat','rb') as infile:
print "1:" + str(marshal.load(infile))
print "2:" + str(marshal.load(infile))
def main():
writedump():
readdump()
if __name__ == '__main__':
main()
■cPickleを使う
import os,cPickle
def writedump():
data1 = '0'*100000
data2 = ['hori','suzuki','yamada']
with open('datafile_pickle.dat','wb') as outfile:
cPickle.dump(obj = data1,file = outfile, protocol = 2)
cPickle.dump(obj = data2,file = outfile, protocol = 2)
def readdump():
with open('datafile_pickle.dat','rb') as infile:
print "1:" + str(cPickle.load(infile))
print "2:" + str(cPickle.load(infile))
def main():
writedump()
readdump()
if __name__ == '__main__':
main()
■さらにZip圧縮する
import os,cPickle,gzip
def writedump():
data1 = '0'*100000
data2 = ['hori','suzuki','yamada']
outfile = gzip.open('datafile_pickle_zip.zip','wb')
cPickle.dump(obj = data1,file = outfile, protocol = 2)
cPickle.dump(obj = data2,file = outfile, protocol = 2)
outfile.close()
def readdump():
infile = gzip.open('datafile_pickle_zip.zip','rb')
print "1:" + str(cPickle.load(infile))
print "2:" + str(cPickle.load(infile))
infile.close()
def main():
writedump()
readdump()
if __name__ == '__main__':
main()
0 件のコメント:
コメントを投稿