python練習_module02-1-員工信息表
阿新 • • 發佈:2017-05-10
註意 最大 空格 n) 輸入 找到 true 之間 進行
python 操作 員工信息表
- 要求:
-
可進行模糊查詢,語法至少支持下面3種:
select name,age from staff_table where age > 22
select * from staff_table where dept = "IT"
select * from staff_table where enroll_date like "2013"查到的信息,打印後,最後面還要顯示查到的條數
-
可創建新員工紀錄,以phone做唯一鍵,staff_id需自增
-
可刪除指定員工信息紀錄,輸入員工id,即可刪除
-
可修改員工信息,語法如下:
UPDATE staff_table SET dept="Market" WHERE where dept = "IT"
註意:以上需求,要充分使用函數,請盡你的最大限度來減少重復代碼 - 達成要求:
- 查詢命令的各單詞之間可以有任意數量的空格
- 查詢時不設定條件(where語句)也可以執行
- 錯誤信息高亮顯示
- 代碼:
#! /usr/bin/env python3 # Author:Jailly import pickle,re # 查找 def search(search_condition): res = [] # 儲存查找結果 # where條件存在 if search_condition: # 三種條件格式 m1 = re.search(r‘age\s+(>|<)\s+(0|([1-9]\d*))‘, search_condition) m2 = re.search(r‘‘‘ (?P<category>(staff_id)|(name)|(age)|(phone)|(dept)|(enroll_date)) \s+=\s+ (?P<value>.*) ‘‘‘, search_condition, re.X) m3 = re.search(r‘‘‘ (?P<category>(staff_id)|(name)|(age)|(phone)|(dept)|(enroll_date)) \s+like\s+ (?P<value>[1-9]\d*) ‘‘‘, search_condition, re.X) # age <|> \d 的情況 if m1: # 篩選條目 for i in staff_table: info_dict = dict(zip([‘staff_id‘, ‘name‘, ‘age‘, ‘phone‘, ‘dept‘, ‘enroll_date‘], i)) if eval(m1.group().replace(‘age‘, str(i[2]))): res.append(info_dict) # category = \w+ 的情況 elif m2: # 篩選條目 for i in staff_table: # 對應信息表中每個條目的字典 info_dict = dict(zip([‘staff_id‘, ‘name‘, ‘age‘, ‘phone‘, ‘dept‘, ‘enroll_date‘], i)) if str(info_dict[m2.group(‘category‘)]) == m2.group(‘value‘).strip(): res.append(info_dict) # category like \w+ 的情況 elif m3: for i in staff_table: info_dict = dict( zip([‘staff_id‘, ‘name‘, ‘age‘, ‘phone‘, ‘dept‘, ‘enroll_date‘], i)) # 對應每個條目的字典 if info_dict[m3.group(‘category‘)].count(m3.group(‘value‘)) or str(info_dict[m3.group(‘category‘)]).count(str(m3.group(‘value‘))): res.append(info_dict) # 條件格式不正確的情況 else: print(‘\033[1;31mwhere語句格式錯誤!\033[0m‘) # 無where條件 else: for i in staff_table: info_dict = dict(zip([‘staff_id‘, ‘name‘, ‘age‘, ‘phone‘, ‘dept‘, ‘enroll_date‘], i)) res.append(info_dict) return res # 顯示查找結果 def show(res, search_categories): if res: for i in res: # 查詢所有條目 * if search_categories.strip() == ‘*‘: print(‘,‘.join([str(i[‘staff_id‘]), i[‘name‘], str(i[‘age‘]), i[‘phone‘], i[‘dept‘], i[‘enroll_date‘]])) # 查詢指定條目 name,age,... else: category_list = search_categories.split(‘,‘) for j in category_list: print(i[j.strip()], end=‘,‘) print() print(‘\n共查找到%d條記錄\n‘ % len(res)) # 保存文件 def save(): with open(‘staff_table.pkl‘, ‘wb‘) as f: pickle.dump(staff_table, f) if __name__ == ‘__main__‘: while 1: with open(‘staff_table.pkl‘, ‘rb‘) as f: staff_table = pickle.load(f) cmd = input(‘command -> ‘) # 查詢指令 search_cmd = re.search(r‘‘‘ ^\s*select\s+ (?P<categories>\*| ( ((staff_id)|(name)|(age)|(phone)|(dept)|(enroll_date)) (\s*,\s*((staff_id)|(name)|(age)|(phone)|(dept)|(enroll_date)))? (\s*,\s*((staff_id)|(name)|(age)|(phone)|(dept)|(enroll_date)))? (\s*,\s*((staff_id)|(name)|(age)|(phone)|(dept)|(enroll_date)))? (\s*,\s*((staff_id)|(name)|(age)|(phone)|(dept)|(enroll_date)))?) ) \s+from\s+staff_table (\s+where\s+ (?P<condition>.*))?$‘‘‘,cmd,re.X) # 添加指令 insert_cmd = re.search(r‘‘‘ ^\s*[\w ]+, # 姓名 [1-9]\d*, # 年齡 \d{11}, # 手機號碼 [\w ]+, # 部門 ((20)|(19))\d{2}-((10)|(11)|(12)|(0[1-9]))-(([0-2][1-9])|(3[01]))$ # 入職日期 ‘‘‘,cmd,re.X) # 刪除指令 delete_cmd = re.search(r‘^\d+$‘,cmd) # 更改指令 update_cmd = re.search(r‘‘‘ ^update\s+staff_table\s+set\s+ (?P<category_update>(name)|(age)|(phone)|(dept)|(enroll_date))\s+ =\s+ (‘|")(?P<new_value>.*)(‘|") (\s+where\s+ (?P<condition>.*))?$ ‘‘‘,cmd,re.X) # 查詢 if search_cmd: search_categories = search_cmd.group(‘categories‘) search_condition = search_cmd.group(‘condition‘) res = search(search_condition) show(res,search_categories) # 添加 elif insert_cmd: insert_list = insert_cmd.group().split(‘,‘) repeat_flag = 0 for i in staff_table: if insert_list[2] == i[3]: print(‘\033[1;31m手機號碼已存在!\033[0m‘) repeat_flag = 1 break else: insert_list.insert(0,( staff_table[-1][0] + 1 ) ) staff_table.append(insert_list) save() print(‘添加成功!‘) # 刪除 elif delete_cmd: for i in staff_table: if delete_cmd.group() == str(i[0]): staff_table.remove(i) save() print(‘刪除成功!‘) break else: print(‘\033[1;31m該staff_id不存在\033[0m‘) # 修改 elif update_cmd: search_condition = update_cmd.group(‘condition‘) res = search(search_condition) for i in range(len(staff_table)): for j in res: if staff_table[i][0] == j[‘staff_id‘]: category_update = update_cmd.group(‘category_update‘) new_value = update_cmd.group(‘new_value‘).strip() j[category_update] = int(new_value) if category_update == ‘age‘ else new_value # 保證順序,不能用list(j.values()) staff_table[i] = list([j[‘staff_id‘],j[‘name‘],j[‘age‘],j[‘phone‘],j[‘dept‘],j[‘enroll_date‘]]) save() print(‘修改成功!‘) # 退出 elif cmd == ‘q‘: break # 輸入格式不正確的情況 else: print(‘\033[1;31m輸入格式錯誤\033[0m‘)
python練習_module02-1-員工信息表