|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
def readFileToDic(self):
# 读取文件,写入到字典中
f = open(self.path, 'r', encoding='utf-8')
clist = f.readlines()
f.close()
index = 0
shopdic = {}
while index < len(clist):
# 将每一行的字符串进行分割,存放到新的列表中
ctlist = clist[index].replace('\n', "").split("|")
# 将每行的内容存放到一个对象中
good = Goods(ctlist[0], ctlist[1], int(ctlist[2]))
# 将对向存放到集合中
shopdic[good.id] = good
index = index + 1
return shopdic
def writeContentFile(self):
# 将内存当中的信息写入到文件当中
str1 = ''
for key in self.shopdic.keys():
good = self.shopdic[key]
ele = good.id + "|" + good.name + "|" + str(good.price) + "\n"
str1 = str1 + ele
f = open(self.path, 'w', encoding='utf-8')
f.write(str1)
f.close()
|
|