pkl文件是python里面保存文件的一种格式,如果直接打开会显示一堆序列化的东西。正确的操作方式是使用Pickle模块。Pickle模块将任意一个Python对象转换成一系统字节,这个操作过程叫做串行化对象。
打开方式:
import cPickle as pickle
f = open('path')
info = pickle.load(f)
print info #show file
写入方式:
import cPickle as pickle
temp = [1,2,3,'adb','a']
with open('filename','w') as f:
pickle.dump(temp, f, pickle.HIGHEST_PROTOCOL)
样例代码:
import cPickle as pickle
class people:
id = 0
name='y'
def add(self):
self.id+=1
self.name="yang"
#man = people()
#print man.name
kk = [people(), people()]
kk[1].add()
#print man.name
with open('test.txt','wb') as f:
pickle.dump(kk, f)
fid = open('test.txt')
temp = pickle.load(fid)
fid.close()
print temp[1].name
print temp[0].name