码迷,mamicode.com
首页 > 其他好文 > 详细

序列化写到类里

时间:2016-05-11 23:48:39      阅读:228      评论:0      收藏:0      [点我收藏+]

标签:

import os
import pickle
class File_handle(object):
def __init__(self,File):#判断文件是否存在
self.__file = File
if os.path.exists(self.__file) == False:
a={}
self.wirt(**a)

def read(self):# 读取数据
with open(self.__file,‘rb‘) as f:
locking_r = pickle.load(f)
return locking_r

def wirt(self,**args):# 保存数据
with open(self.__file,‘wb‘) as f:
pickle.dump(args,f)
return ‘success‘

Dict_chengji={‘zhangsan‘:90,‘lisi‘:99}#字典1
Dict_xingbie={‘zhangsan‘:‘nan‘,‘lisi‘:‘nv‘}#字典2

P_1=File_handle(‘./chengji‘)#实例化1 文件名chengji
P_1.wirt(**Dict_chengji)#写入字典1
print (P_1.read())#读取字典1

P_2=File_handle(‘./xingbie‘)
P_2.wirt(**Dict_xingbie)
print(P_2.read())

序列化写到类里

标签:

原文地址:http://www.cnblogs.com/illn/p/5483991.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!