bet365体育app下载_外围bet365 网址_bet365娱乐在线先锋网?bet365体育app下载_外围bet365 网址_bet365娱乐在线片段及技术文章聚合

Python中的.pkl文件

技术标签:?python??.pkl

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)

样例bet365体育app下载_外围bet365 网址_bet365娱乐在线:

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

?

原文地址:Python中的.pkl文件