员工信息代码之---增删改查(装饰器、生成器、文件操作)
import os dic = {'id':0,'name':1,'age':2,'phone':3,'job':4} user_dic = {'name':'admin','passwd':'123456'} #读取文件---将文件中的内容整理到内存中 def get_line(filename): with open(filename,mode='r+',encoding='utf-8') as f: for line in f: line = line.strip() line_lst = line.split(',') yield line_lst # # # # #条件筛选 def condition_filter(condition): # print(condition) condition=condition.strip() if '>' in condition: col,val = condition.split('>') g = get_line('userinfo') for line_lst_1 in g: if int(line_lst_1[dic[col]]) > int(val): yield line_lst_1 elif '<' in condition: col,val = condition.split('<') g = get_line('userinfo') for line_lst_1 in g : if int(line_lst_1[dic[col]]) < int(val): yield line_lst_1 else: col,val = condition.split('=') g = get_line('userinfo') for line_lst_1 in g: if int(line_lst_1[dic[col]]) == int(val): yield line_lst_1 # # #展示符合条件的员工信息 def views(view_list,staff_g): if '*' in view_list: view_list = dic.keys() for staff_info in staff_g: for i in staff_info: print(i,end=' ') print(end='\n') # print(view_list) for staff_info in staff_g: print(staff_info[dic[view_list[0]]],staff_info[dic[view_list[1]]]) # # # # # #接受用户输入的信息 # ret = input('select name,age where age=23') ret = 'select name,job where age>22' view,condition = ret.split('where') view = view.replace('select','').strip() view_list = view.split(',') # print(view_list,condition) # g = condition_filter(condition) #此处3行代码已经放在最下面装饰器处统一调用 # views(view_list,g) # # #修改表中对应数据 def update(): # up_data = input('请输入需要更新的数据,例如:set age=100 where name=Egon: ') up_data = 'set age=100 where name=Egon' if 'set' and 'where' in up_data: cont,condit = up_data.strip().split('where') print(cont,condit) cont = cont.split('set') cont = cont[1].strip() update_name,update_value = cont.split('=') print(update_name,update_value) names,value = condit.strip().split('=') print(names,value) with open('userinfo',encoding='utf-8') as f,open('userinfo.bak',mode='w+',encoding='utf-8') as f1: for line in f: line_list = line.strip().split(',') # print(line_list) if line_list[dic[names]] == value: before_value = line_list[dic[update_name]] line = line.replace(before_value,update_value) f1.write(line) os.remove('userinfo') os.rename('userinfo.bak','userinfo') # update('userinfo') # # #删除表中对应数据 def delete(): # del_data = input('请输入要删除的ID,例如:delete * where id=:') del_data = 'delete * where id=2' if 'delete' and 'where' in del_data: delete_content,delete_cond = del_data.strip().split('where') #以where分割 # print(delete_content,delete_cond) if delete_content.strip() and delete_cond.strip(): #判断delete_content和delete_cond是否为空,不为空执行下面程序 name,value = delete_cond.strip().split('=') #以‘=’分割 print(name,value) with open('userinfo',mode='r',encoding='utf-8') as f: for line in f: line_list = line.strip().split(',') #以逗号分割以列表形式展示 # print(line_list) #打印循环的文件 if line_list[dic[name.strip()]] == value.strip(): line = line.replace(line,'') with open('userinfo.bak', mode='a', encoding='utf-8') as f1: f1.write(line) os.remove('userinfo') os.rename('userinfo.bak','userinfo') #数据增加 # dic = {'id':0,'name':1,'age':2,'phone':3,'job':4} def add(): s = 'add children,26,15055555588,Student' add_user = s.strip().split('add') # print(add_user) user_str = '' for i in add_user: user_str += i user_str = user_str.strip() # print(user_str) with open('userinfo',mode='r',encoding='utf-8') as f: for line in f: line = line.strip() line_list = line.strip().split(',') user_id = int(line_list[0]) with open('userinfo','a+',encoding='utf-8') as f1: f1.write('\n'+str(user_id+1)+','+user_str) #装饰器函数 ---验证用户登录 # def check(): # print(1) # def delete(): # print(2) # def add(): # print(3) # def update(): # print(4) def wrapper(func): #func=main def inner(*args,**kwargs): username = input('请输入您的用户名:') password = input('请输入您的密码:') if username.strip() ==user_dic['name'] and password.strip() ==user_dic['passwd']: print('登录成功',end='') ret = func(*args,**kwargs) return ret else:print('用户名或密码错误,请重新输入:') return inner @wrapper #main=login(main) main=inner def login(): msg = {'1':condition_filter,'2':delete,'3':add,'4':update} print(''' 1:查询 2:删除 3:添加 4:更新 ''') num = input('请选择要执行的操作').strip() if int(num) == 1: g = msg[num](condition) # g = condition_filter(condition) views(view_list, g) else: msg[num]() login()