现要求你写一个简单的员工信息增删改查程序,需求如下:
当然此表你在文件存储时可以这样表示
1,Alex Li,22,13651054608,IT,2013-04-01 2,Jack Wang,28,13451024608,HR,2015-01-07 3,Rain Wang,21,13451054608,IT,2017-04-01 4,Mack Qiao,44,15653354208,Sales,2016-02-01 5,Rachel Chen,23,13351024606,IT,2013-03-16 6,Eric Liu,19,18531054602,Marketing,2012-12-01 7,Chao Zhang,21,13235324334,Administration,2011-08-08 8,Kevin Chen,22,13151054603,Sales,2013-04-01 9,Shit Wen,20,13351024602,IT,2017-07-03 10,Shanshan Du,26,13698424612,Operation,2017-07-02
1.可进行模糊查询,语法至少支持下面3种查询语法:
find name,age from staff_table where age > 22 find * from staff_table where dept = "IT" find * from staff_table where enroll_date like "2013"
2.可创建新员工纪录,以phone做唯一键(即不允许表里有手机号重复的情况),staff_id需自增
语法: add staff_table Alex Li,25,134435344,IT,2015-10-29
3.可删除指定员工信息纪录,输入员工id,即可删除
语法: del from staff where id=3
4.可修改员工信息,语法如下:
UPDATE staff_table SET dept="Market" WHERE dept = "IT" 把所有dept=IT的纪录的dept改成Market UPDATE staff_table SET age=25 WHERE name = "Alex Li" 把name=Alex Li的纪录的年龄改成25
5.以上每条语名执行完毕后,要显示这条语句影响了多少条纪录。 比如查询语句 就显示 查询出了多少条、修改语句就显示修改了多少条等。
注意:以上需求,要充分使用函数,请尽你的最大限度来减少重复代码!
import os def search(data): data = data.split(' ') con = data[7] asp = data[5] count = 0 with open('staff_table','r',encoding='utf-8') as f: if asp == 'age': for line in f: if int(line.split(',')[2]) >= int(con): print(line.split(',')[1],line.split(',')[2]) count += 1 elif asp == 'dept': for line in f: if line.split(',')[4] in con: print(line.split(',')[1],line.split(',')[4]) count += 1 elif asp == 'enroll_date': for line in f: if line.split(',')[5].split('-')[0][0:4] in con[1:-1]: print(line.split(',')[1],line.split(',')[5]) count += 1 print('查询结束,共查到符合条件的信息 %d 条 ' % count) return data def add(data): list_data = data.strip().split(',') list_all = [] f = open('staff_table','r+',encoding='utf-8') for line in f: list_all.append(line.strip().split(',')[3]) #把手机号添加到list_all列表中 if list_data[2] in list_all: print("该用户已经存在") f.close() else: for line in f: f.write(line) staff_id = str(len(list_all)+1) list_data.insert(0, str(staff_id)) f.write(','.join(list_data)) #把列表转成字符串写进去 f.close() print('添加成功') def remove(data): staff_id = data.strip().split(' ')[6] f = open('staff_table','r',encoding='utf-8') f1 = open('new_staff_table','w',encoding='utf-8') for line in f: in_list = line.split(',') if in_list[0] < staff_id: f1.write(line) elif in_list[0] > staff_id: in_list[0] = str(int(in_list[0])-1) f1.write(','.join(in_list)) else: continue f.close() f1.close() os.remove('staff_table') os.rename('new_staff_table','staff_table') print('删除成功') def change(data): ind = data.split(' ')[3] count = 0 if ind[:4] == 'dept': old=data.strip().split(' ')[7][1:-1] new=data.strip().split(' ')[3][-7:-1] f=open('staff_table','r',encoding='utf-8') f1=open('new_staff_table','w',encoding="utf-8") for line in f: if old in line: line=line.replace(old,new) count += 1 f1.write(line) f.close() f1.close() os.remove('staff_table') os.rename('new_staff_table','staff_table') print('修改成功') elif ind[:3] == 'age': f = open("staff_table",'r+',encoding='utf-8') f1 = open('new_staff_table','w',encoding="utf-8") for line in f: if 'Alex Li' in line: line = line.replace(line[10:12],'22') count += 1 f1.write(line) f.close() f1.close() os.remove('staff_table') os.rename('new_staff_table','staff_table') print('修改成功') print('共修改符合条件的信息 %d 条 ' % count) if __name__ == "__main__": msg = """ 欢迎来到员工信息查询系统! 操作选项: 1、模糊查询员工信息(查询语法一.select name,age from staff_table where age > 22 二.select * from staff_table where dept = IT 三.find * from staff_table where enroll_date like "2013") 2、新增员工信息(添加语法:name,age,phone,dept,enroll-date(例如:kris,18,18271182769,IT,2018-03-12)) 3、删除指定员工信息(删除语法:delete from staff_table where staff_id = 11) 4、修改员工信息(修改语法1.UPDATE staff_table SET dept='Market' WHERE dept = 'IT' 2.UPDATE staff_table SET age=25 WHERE name = "Alex Li") 5、quit退出 """ msg_dict = { "1": search, "2": add, "3": remove, "4": change, "5": exit, } while True: print(msg) choice = input("请输入序号>>:") if len(choice) == 0 or choice not in msg_dict: continue if choice =='5': break data = input("请输入操作信息>>:").strip() msg_dict[choice](data)