現要求你寫一個簡單的員工資訊增刪改查程式.
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,MacQiao,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種:
1.select name,age from staff_table where age > 22
2.select * from staff_table where dept = IT
3.查到的資訊,列印後,最後面還要顯示查到的條數
2.可建立新員工紀錄,以phone做唯一鍵,staff_id需自增
3.可刪除指定員工資訊紀錄,輸入員工id,即可刪除
4.可修改員工資訊,語法如下:
1.UPDATE staff_table SET dept= 無 where dept = IT
import os def table_info(): # 讀取檔案,並轉化為列表 date_file = open('data.txt','r',encoding='utf-8') lines = date_file.readlines() data = [] for line in lines: data.append(line.split(',')) return data date_file.close() table_info() def qurey1(x): count = 0 for line in table_info(): if line[2] > '22': d1[count] = [line[1], line[2]] count += 1 print('name', ' ', 'age') for i in d1.keys(): print(d1[i][0], ',', d1[i][1]) print('滿足本次查詢的個數為:', count) def qurey2(x): count = 0 str = x[1:3] for line in table_info(): if line[4] == str: d1[count] = line count += 1 for i in d1.values(): for m in i: print(m, end=' ') print('滿足本次查詢的個數為:', count) def qurey3(x): count = 0 str = x[1:5] for line in table_info(): if line[-1][:4] == str: d1[count] = line count += 1 for i in d1.values(): for m in i: print(m, end=' ') print('滿足本次查詢的個數為:', count) def delete(): while True: count = 0 print("退出請輸入Q") input1 = input('請輸入新建命令的SQL語句:').strip() input_del = input1.split(' ') if input1 == "Q" or input1 == 'q': break else: del_id = input_del[-1].split('=')[-1] with open('data.txt','r',encoding='utf-8') as f_read: lines = f_read.readlines() with open('data.txt','w',encoding='utf-8') as f_wirte: for line in lines: if del_id == line.split(',')[0]: count +=1 continue else: f_wirte.write(line) print('本次刪除的數目為:',count) def new(): while True: print("退出請輸入Q") input_a = input('請輸入新建命令的SQL語句:').strip() input_add = input_a.split(' ') if input_a == 'Q' or input_a == 'q': print('返回到第一層!') break else: name = input_add[2] msg = input_add[3] phone = msg.split(',')[2] count = 0 num = 0 for line in table_info(): count += 1 if int(line[3]) == int(phone): print('您輸入的使用者已存在') newstr = str(count + 1) + ',' + name + ',' f = open('data.txt', 'a', encoding='utf-8') f.writelines('\n ') f.writelines(newstr) f.writelines(msg) f.close() num += 1 print('\033[1;35m 使用者已新增 \033[0m'.center(10, '.')) print('本次操作的命令列數為',num) def modify(): while True: print("""支援以下語句修改員工資訊,包括部門名稱與年齡:') UPDATE staff_table SET dept="Market" WHERE dept = "IT"') UPDATE staff_table SET age=25 WHERE name = "Alex Li"') 退出請按Q""") print('請輸入您要操作的語句:') user_mod = input('<<<').strip() user_msg = user_mod.split(' ')[3].split('=') # 提取年齡 if user_mod == "q" or user_mod == "Q": print('退出到上一層!') break elif user_msg[0] == 'age': aft = user_msg[1] name = user_mod.split('"')[-2] count = 0 with open('data.txt', 'r+', encoding='utf-8') as f1: lines = f1.readlines() with open('data.txt', 'w', encoding='utf-8') as f2: for line in lines: if name in line: bef = line.split(',')[2] line = line.replace(bef, aft) print(1) count += 1 f2.write(line) print('資訊已更新!') print('本次更新資訊數目為:', count) else: input_update = user_mod.split('"') before = input_update[3] after = input_update[1] count = 0 with open('data.txt', 'r+', encoding='utf-8') as f1: lines = f1.readlines() with open('data.txt', 'w', encoding='utf-8') as f2: for line in lines: if before in line: line = line.replace(before, after) count += 1 f2.write(line) print('資訊已更新!') print('本次更新資訊數目為:', count) print('歡迎來到員工查詢系統'.center(40,'*')) while True: print('''請根據以下提示輸入對應指令序號:') 1.查詢員工資訊') 2.建立員工資訊') 3.刪除員工資訊') 4.修改員工資訊''') input_num = int(input('>>>>>').strip()) if input_num == 1: while True: print('退出請按Q') print('我們支援以下查詢語句'.center(40,'-')) print(''' 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"' 請輸入您的查詢語句:''') input_sent = input('<<<<').strip() input_sent1 = input_sent.split(' ') d1 = {} print("本次查詢的結果如下:".center(20, "-")) if input_sent == 'find name,age from staff_table where age > %s' % (input_sent1[-1]): qurey1(input_sent1[-1]) elif input_sent == 'find * from staff_table where dept = %s' % (input_sent1[-1]): qurey2(input_sent1[-1]) elif input_sent == 'find * from staff_table where enroll_date like %s' % (input_sent1[-1]): qurey3(input_sent1[-1]) elif input_sent == "Q" or input_sent == 'q': break else: print('您的輸入格式有誤,請重新輸入!') elif input_num == 2: new() elif input_num == 3: delete() elif input_num == 4: modify() else: print('您輸入有誤,請重新輸入')
問題:table_info 函式其實可以改成modify等函式的呼叫方式