文件名:staff_table.txt
文件内容:
1,Alex Li,22,13651054608,market,2013-04-01 2,Jack Wang,28,13451024608,HR,2015-01-07 3,Rain Wang,21,13451054608,market,2017-04-01 4,Mack Qiao,44,15653354208,Sales,2016-02-01 5,Rachel Chen,23,13351024606,market,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,market,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. 以上每条语名执行完毕后,要显示这条语句影响了多少条纪录。 比如查询语句 就显示 查询出了多少条、修改语句就显示修改了多少条等。
top_list = [‘查询‘,‘修改‘] # 第一级菜单
# 提取号码函数: 用于唯一键判断 def info_category(): global staff_phone staff_phone = [] f = open(‘staff_table.txt‘, ‘r‘, encoding=‘utf-8‘) for line in f: id, name, age, phone, dept, enroll_date = line.split(‘,‘) staff_phone.append(phone)
# 打印列表清单函数 def print_list(list): global choice print(‘请输入查询编号‘.center(50, ‘-‘)) for k, i in enumerate(list): print(‘%s. %s‘ % (k, i)) choice = int(input(‘查询编号:‘).strip())
#查询函数: def info_search(): count = 0 # 用于查询计数 search_list = [‘Age‘, ‘Dept‘, ‘Enroll Date‘] print_list(search_list) # 调用打印列表函数 find = input(‘输入查询信息:‘).strip() f = open(‘staff_table改.txt‘, ‘r‘, encoding=‘utf-8‘) for line in f: id, name, age, phone, dept, enroll_date = line.split(‘,‘) # 查询年龄 if choice == 0: if int(age) > int(find): count += 1 print(name, age) # 查询dept elif choice == 1: if dept == find: count += 1 print(line) # 查询入职日期 elif choice == 2: if enroll_date.startswith(find): count += 1 print(line) return count
# 修改(增、删、update)信息函数: def info_modify(): import os modify_list = [‘添加‘,‘删除‘,‘更新‘] print_list(modify_list) # 调用打印函数 # 创建一个新文件,用于修改文件内容 f1_name = ‘staff_table.txt‘ f2_name = ‘staff_table.txt.new‘ # (自己写出错的地方: 忘了先创建两个文件名) f1 = open(f1_name,‘r‘,encoding=‘utf-8‘) f2 = open(f2_name,‘w‘,encoding=‘utf-8‘) # (新创建的文件需要时‘w‘(写)的模式) # 添加 def info_add(): id_no = 1 info_category() for line in f1: f2.write(line) # 用于保证:即使什么都没添加,原有的文件也不会被新文件覆盖为空 id_no += 1 # 用于员工ID自动加1 phone = input(‘输入手机号:‘).strip() if phone not in staff_phone: name = input(‘Name:‘) age = int(input(‘Age:‘)) dept = input(‘Dept:‘) enroll_date = input(‘Enroll Date:‘) f2.write(‘\n%s,%s,%s,%s,%s,%s‘ % (id_no, name, age, phone, dept, enroll_date)) else: print(‘号码已存在‘) # 删除 def info_del(del_info): for line in f1: if line.startswith(del_info): # 输入员工ID时需要多加一个逗号 pass else: f2.write(line) # 更新 def info_update(): count = 0 # 用于更新条数计数 update_list = [‘dept‘, ‘age‘] print_list(update_list) # 调用打印列表函数 # 更新Dept函数 if choice == 0: old_content = input(‘需要更新的部门:‘) new_content = input(‘新部门名称:‘) for line in f1: if old_content in line: count +=1 line = line.replace(old_content, new_content) #( 要把替换的内容再赋值给line) f2.write(line) print(‘更新条数:‘,count) # 根据名字更新年龄函数 elif choice == 1: name_symbol = input(‘修改年龄的人名:‘).strip() new_age = input(‘输入新年龄:‘).strip() for line in f1: if name_symbol in line: count += 1 line = line.split(‘,‘) # 此时line是一个列表 line[2] = new_age #把列表line中索引值为2的元素改为new_age line = ‘,‘.join(line) # 此时line又变成了字符串 f2.write(line) print(‘更新条数:‘,count) if choice == 0: info_add() elif choice == 1: del_info = input(‘删除信息id:‘) info_del(del_info) elif choice == 2: info_update() else: for line in f1: f2.write(line) # 用于保证:即使什么都没修改,原有的文件也不会被新文件覆盖为空 f1.close() f2.close() # (os.replace之前,应该先把两个文件close掉) os.replace(f2_name, f1_name) print_list(top_list) # 调用打印列表函数 if choice == 0: print(‘查询匹配条数:‘,info_search()) else: info_modify()