現要求寫一個簡單的員工資訊增删改查程式,需求如下:

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.以上每條語名執行完畢後,要顯示這條語句影響了多少條紀錄。 比如查詢語句 就顯示 查詢出了多少條、修改語句就顯示修改了多少條等。
注意:以上需求,要充分使用函數,請盡你的最大限度來減少重複代碼!
一:題目思路圖:
二,程式概述:
**概述:**
本次作業檔案夾一共包含了以下4個檔案:
流程圖:員工資訊表思路流程圖
程式檔案: run_program.py
使用者資訊檔案:staff_info.txt
程式說明檔案:README.md
**一,程式功能**
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.以上每條語名執行完畢後,要顯示這條語句影響了多少條紀錄。 比如查詢語句 就顯示 查詢出了多少條、修改語句就顯示修改了多少條等。
**二,部分變量說明**
prompt_func() 歡迎登入的函數名
initial_employee_information() 初始化員工資訊的函數名
find_func() 查找函數名
add_func() 添加函數名
del_func() 删除函數名
update_func() 更新函數名
main() 主函數
staff_infofile 讀取檔案後,存放檔案内容的變量
user_input 使用者輸入
core_message 使用者添加的資訊内容
DATA_STAFF 初始化資料,定義成常量
after_update_name 更新後的名稱
after_update_content 更新後的内容
before_update_name 更新前的名稱
before_update_content 更新前的内容
**三,運作代碼**
本程式的開發環境是python3.x
運作後,根據控制台顯示的提示資訊執行
三,主程式:
#_*_coding:utf-8_*_
def prompt_func():
return ('''
歡迎來到員工資訊查詢系統!
操作選項:
1、模糊查詢員工資訊
2、新增員工資訊
3、删除指定員工資訊
4、修改員工資訊
5、quit傳回上一級
''')
def initial_employee_information():
'''
初始化員工資訊資料,即把員工資訊讀到記憶體裡面
:return:{'id': ['1', '2', '4', '5', '6', '7', '8', '9', '10'], 'name': ['Alex Li', 'Jack Wang', 'Mack Qiao', 'Rachel Chen', 'Eric Liu', 'Chao Zhang', 'Kevin Chen', 'Shit Wen', 'Shanshan Du'], 'age': ['22', '28', '44', '23', '19', '21', '22', '20', '26'], 'phone': ['13651054608', '13451024608', '15653354208', '13351024606', '18531054602', '13235324334', '13151054603', '13351024602', '13698424612'], 'depart': ['IT', 'HR', 'Sales', 'IT', 'Marketing', 'Administration', 'Sales', 'IT', 'Operation'], 'enrolled_date': ['2013-04-01\n', '2015-01-07\n', '2016-02-01\n', '2013-03-16\n', '2012-12-01\n', '2011-08-08\n', '2013-04-01\n', '2017-07-03\n', '2017-07-02']}
'''
data_staff = {}
staff_list = ['id', 'name', 'age', 'phone', 'depart', 'enrolled_date']
for i in staff_list:
data_staff[i] = []
# print_log(data_staff)
staff_infofile = open('staff_info.txt','r+',encoding='utf-8')
for line in staff_infofile:
staff_id, staff_name, staff_age, staff_phone, staff_depart, staff_date = line.split(',')
data_staff['id'].append(staff_id)
data_staff['name'].append(staff_name)
data_staff['age'].append(staff_age)
data_staff['phone'].append(staff_phone)
data_staff['depart'].append(staff_depart)
data_staff['enrolled_date'].append(staff_date)
staff_infofile.close()
return data_staff
DATA_STAFF = initial_employee_information()
def find_func():
while True:
print('''
***************************************************
指令行示例:
find name age where age > 20
find * from staff_table where dept IT
find * from staff_table where enroll_date like 2013
***************************************************
''')
user_input = input("請輸入您要查詢員工資訊的正确的文法(如果想傳回上一層,請按q):").split()
for index,age in enumerate(DATA_STAFF['age']):
if '>' in user_input:
if age > user_input[-1]:
print(DATA_STAFF['name'][index],DATA_STAFF['age'][index])
elif '<' in user_input:
if age < user_input[-1]:
print(DATA_STAFF['name'][index],DATA_STAFF['age'][index])
elif '=' in user_input:
if age == user_input[-1]:
print(DATA_STAFF['name'][index],DATA_STAFF['age'][index])
for index,depart in enumerate(DATA_STAFF['depart']):
if depart in user_input:
print(DATA_STAFF['id'][index], DATA_STAFF['name'][index],
DATA_STAFF['age'][index],DATA_STAFF['phone'][index],
DATA_STAFF['depart'][index], DATA_STAFF['enrolled_date'][index])
for index,enrolled_date in enumerate(DATA_STAFF['enrolled_date']):
enrolled_date =enrolled_date.split('-')[0]
if enrolled_date in user_input and 'like' in user_input:
print(DATA_STAFF['id'][index], DATA_STAFF['name'][index],
DATA_STAFF['age'][index],DATA_STAFF['phone'][index],
DATA_STAFF['depart'][index], DATA_STAFF['enrolled_date'][index])
if user_input == 'q'.split():
break
def add_func():
while True:
print('''
***************************************************************
員工錄入示例:add staff_table Alex Li,25,134435344,IT,2015-10-29
***************************************************************
''')
staff_infofile = open('staff_info.txt','a+',encoding='utf-8')
user_input = input("請輸入您要增加員工資訊的正确的文法(如果想傳回上一層,請按q):\n").split('staff_table')
if user_input == 'q'.split():
break
core_message = ','.join(user_input)
core_message =core_message.split(',')[1:]
STAFF_ID = int(DATA_STAFF['id'][-1]) +1
DATA_STAFF['id'].append(STAFF_ID)
for iphone in DATA_STAFF['phone']:
if core_message[2] ==iphone:
print("手機不允許重複,請重新添加")
add_func()
else:
pass
staff_infofile.write('\n' + str(STAFF_ID) + ',' + ','.join(core_message))
staff_infofile.close()
print('\033[1;31m 影響了1條記錄 \033[0m')
def del_func():
print('''
***********************************
删除示例:del from staff where id=3
***********************************
''')
del_staffid = input("請輸入您要删除的員工id的文法:\n ")
if len(del_staffid) ==26 or len(del_staffid) ==27:
del_staffid = del_staffid.split('=')
count = 1
del_left, del_right = del_staffid
if del_right in DATA_STAFF['id']:
staff_index = DATA_STAFF['id'].index(del_right)
staff_infofile = open('staff_info.txt', 'w', encoding='utf_8')
print('\033[31;1m員工 ' + DATA_STAFF['name'][staff_index] + ' 已經删除\033[0m')
del DATA_STAFF['id'][staff_index]
del DATA_STAFF['name'][staff_index]
del DATA_STAFF['age'][staff_index]
del DATA_STAFF['phone'][staff_index]
del DATA_STAFF['depart'][staff_index]
del DATA_STAFF['enrolled_date'][staff_index]
while True:
staff_wr = DATA_STAFF['id'][count] + ',' + DATA_STAFF['name'][count] + ','\
+ DATA_STAFF['age'][count] + ',' + DATA_STAFF['phone'][count] + ','\
+ DATA_STAFF['depart'][count] + ',' + DATA_STAFF['enrolled_date'][count]
staff_infofile.write(staff_wr)
count += 1
if count == len(DATA_STAFF['id']):
break
staff_infofile.close()
else:
print("\033[31;1m員工資訊表中無此員工的資訊,請重新輸入\033[0m")
del_func()
else:
print("\033[31;1m輸入的文法有誤,請重輸!\33[0m")
del_func()
print('\033[1;31m 影響了1條記錄 \033[0m')
def update_func():
print('''
*************************************************************
示例:UPDATE staff_table SET dept="Market" WHERE dept = "IT"
UPDATE staff_table SET age=25 WHERE name = "Alex Li"
*************************************************************
''')
update_staff = input("請輸入您要更新的員工資訊的文法:\n ")
user_update = update_staff.split('SET')
update_staff_left,update_staff_right = user_update
user_update_finally = user_update[-1].strip().split('WHERE')
after_update = user_update_finally[0]
before_update = user_update_finally[1]
after_update_name,after_update_content = after_update.split('=')
before_update_name, before_update_content = before_update.split('=')
if after_update_name.strip() == before_update_name.strip():
for dept in DATA_STAFF['depart']:
if dept == eval(before_update_content):
DATA_STAFF['depart'][DATA_STAFF['depart'].index(eval(before_update_content))] \
= eval(after_update_content)
count = 0
staff_infofile = open('staff_info.txt', 'w', encoding='utf_8')
while True:
staff_wr = DATA_STAFF['id'][count] + ',' + DATA_STAFF['name'][count] + ',' \
+ DATA_STAFF['age'][count] + ',' + DATA_STAFF['phone'][count] + ',' \
+ DATA_STAFF['depart'][count] + ',' + DATA_STAFF['enrolled_date'][count]
staff_infofile.write(staff_wr)
count += 1
if count == len(DATA_STAFF['id']):
break
staff_infofile.close()
print('\033[1;31m 影響了3條記錄 \033[0m')
else:
for index,name in enumerate(DATA_STAFF['name']):
if name.strip() == eval(before_update_content).strip():
DATA_STAFF['age'][index] = eval(after_update_content)
else:
pass
count = 0
staff_infofile = open('staff_info.txt', 'w', encoding='utf_8')
while True:
staff_wr = str(DATA_STAFF['id'][count]) + ',' + DATA_STAFF['name'][count] + ',' \
+ str(DATA_STAFF['age'][count]) + ',' + str(DATA_STAFF['phone'][count]) + ',' \
+ DATA_STAFF['depart'][count] + ',' + DATA_STAFF['enrolled_date'][count]
staff_infofile.write(staff_wr)
count += 1
if count == len(DATA_STAFF['id']):
break
staff_infofile.close()
print('\033[1;31m 影響了1條記錄 \033[0m')
def main():
while True:
print(prompt_func())
user_input = input("請輸入要執行操作的序号>> ")
if user_input == '1':
print("-------------歡迎進入模糊查詢員工資訊界面----------------")
find_func()
elif user_input == '2':
print("-------------歡迎進入新增員工資訊界面----------------")
add_func()
elif user_input == '3':
print("-------------歡迎進入删除指定員工資訊界面----------------")
del_func()
elif user_input == '4':
print("-------------歡迎進入修改員工資訊界面----------------")
update_func()
else:
print("\033[31;1m輸入的資訊有誤,請重輸!\33[0m")
if __name__ == '__main__':
main()
更新版的:
#_*_coding:utf-8_*_
def prompt_func():
return ('''
歡迎來到員工資訊查詢系統!
操作選項:
1、模糊查詢員工資訊
2、新增員工資訊
3、删除指定員工資訊
4、修改員工資訊
''')
def initial_employee_information():
'''
初始化員工資訊資料,即把員工資訊讀到記憶體裡面
:return:{'id': ['1', '2', '4', '5', '6', '7', '8', '9', '10'], 'name': ['Alex Li', 'Jack Wang', 'Mack Qiao', 'Rachel Chen', 'Eric Liu', 'Chao Zhang', 'Kevin Chen', 'Shit Wen', 'Shanshan Du'], 'age': ['22', '28', '44', '23', '19', '21', '22', '20', '26'], 'phone': ['13651054608', '13451024608', '15653354208', '13351024606', '18531054602', '13235324334', '13151054603', '13351024602', '13698424612'], 'depart': ['IT', 'HR', 'Sales', 'IT', 'Marketing', 'Administration', 'Sales', 'IT', 'Operation'], 'enrolled_date': ['2013-04-01\n', '2015-01-07\n', '2016-02-01\n', '2013-03-16\n', '2012-12-01\n', '2011-08-08\n', '2013-04-01\n', '2017-07-03\n', '2017-07-02']}
'''
data_staff = {}
staff_list = ['id', 'name', 'age', 'phone', 'depart', 'enrolled_date']
for i in staff_list:
data_staff[i] = []
staff_infofile = open('staff_info.txt','r+',encoding='utf-8')
for line in staff_infofile:
staff_id, staff_name, staff_age, staff_phone, staff_depart, staff_date = line.split(',')
data_staff['id'].append(staff_id)
data_staff['name'].append(staff_name)
data_staff['age'].append(staff_age)
data_staff['phone'].append(staff_phone)
data_staff['depart'].append(staff_depart)
data_staff['enrolled_date'].append(staff_date)
staff_infofile.close()
return data_staff
DATA_STAFF = initial_employee_information()
def find_func():
while True:
print('''
***************************************************
指令行示例:
find name age where age > 20
find * from staff_table where dept IT
find * from staff_table where enroll_date like 2013
***************************************************
''')
user_input = input("請輸入您要查詢員工資訊的正确的文法(如果想傳回上一層,請按q):").split()
for index,age in enumerate(DATA_STAFF['age']):
if '>' in user_input:
if age > user_input[-1]:
print(DATA_STAFF['name'][index],DATA_STAFF['age'][index])
elif '<' in user_input:
if age < user_input[-1]:
print(DATA_STAFF['name'][index],DATA_STAFF['age'][index])
elif '=' in user_input:
if age == user_input[-1]:
print(DATA_STAFF['name'][index],DATA_STAFF['age'][index])
for index,depart in enumerate(DATA_STAFF['depart']):
if depart in user_input:
print(DATA_STAFF['id'][index], DATA_STAFF['name'][index],
DATA_STAFF['age'][index],DATA_STAFF['phone'][index],
DATA_STAFF['depart'][index], DATA_STAFF['enrolled_date'][index])
for index,enrolled_date in enumerate(DATA_STAFF['enrolled_date']):
enrolled_date =enrolled_date.split('-')[0]
if enrolled_date in user_input and 'like' in user_input:
print(DATA_STAFF['id'][index], DATA_STAFF['name'][index],
DATA_STAFF['age'][index],DATA_STAFF['phone'][index],
DATA_STAFF['depart'][index], DATA_STAFF['enrolled_date'][index])
if user_input == 'q'.split():
break
def add_func():
while True:
print('''
***************************************************************
員工錄入示例:add staff_table Alex Li,25,134435344,IT,2015-10-29
***************************************************************
''')
staff_infofile = open('staff_info.txt','a+',encoding='utf-8')
user_input = input("請輸入您要增加員工資訊的正确的文法(如果想傳回上一層,請"
"按q):\n").split('staff_table')
if user_input == 'q'.split():
break
core_message = ','.join(user_input)
core_message =core_message.split(',')[1:]
STAFF_ID = int(DATA_STAFF['id'][-1]) +1
DATA_STAFF['id'].append(STAFF_ID)
for iphone in DATA_STAFF['phone']:
if core_message[2] ==iphone:
print("手機不允許重複,請重新添加")
add_func()
else:
pass
staff_infofile.write('\n' + str(STAFF_ID) + ',' + ','.join(core_message))
staff_infofile.close()
def del_func():
while True:
print('''
***********************************
删除示例:del from staff where id=3
***********************************
''')
user_input= input("請輸入您要删除的員工id的文法(如果想傳回上一層,請按q):\n ")
if len(user_input) ==26 or len(user_input) ==27:
del_staffid = user_input.split('=')
count = 1
del_left, del_right = del_staffid
if del_right in DATA_STAFF['id']:
staff_index = DATA_STAFF['id'].index(del_right)
staff_infofile = open('staff_info.txt', 'w', encoding='utf_8')
print('\033[31;1m員工 ' + DATA_STAFF['name'][staff_index] + ' 已經删除\033[0m')
for i in DATA_STAFF:
del DATA_STAFF[i][staff_index]
while True:
staff_wr = DATA_STAFF['id'][count] + ',' + DATA_STAFF['name'][count] + ','\
+ DATA_STAFF['age'][count] + ',' + DATA_STAFF['phone'][count] + ','\
+ DATA_STAFF['depart'][count] + ',' + DATA_STAFF['enrolled_date'][count]
staff_infofile.write(staff_wr)
count += 1
if count == len(DATA_STAFF['id']):
break
staff_infofile.close()
else:
print("\033[31;1m員工資訊表中無此員工的資訊,請重新輸入\033[0m")
del_func()
elif user_input == 'q':
break
else:
print("\033[31;1m輸入的文法有誤,請重輸!\33[0m")
del_func()
def update_func():
while True:
print('''
*************************************************************
示例:UPDATE staff_table SET dept="Market" WHERE dept = "IT"
UPDATE staff_table SET age=25 WHERE name = "Alex Li"
*************************************************************
''')
update_staff = input("請輸入您要更新的員工資訊的文法(如果想傳回上一層,請按q):\n ")
if len(update_staff) >= 50 and len(update_staff) <= 65:
user_update = update_staff.split('SET')
update_staff_left,update_staff_right = user_update
user_update_finally = user_update[-1].strip().split('WHERE')
after_update = user_update_finally[0]
before_update = user_update_finally[1]
after_update_name,after_update_content = after_update.split('=')
before_update_name, before_update_content = before_update.split('=')
if after_update_name.strip() == before_update_name.strip():
for dept in DATA_STAFF['depart']:
if dept == eval(before_update_content):
DATA_STAFF['depart'][DATA_STAFF['depart'].index(eval(before_update_content))] \
= eval(after_update_content)
count = 0
staff_infofile = open('staff_info.txt', 'w', encoding='utf_8')
while True:
staff_wr = DATA_STAFF['id'][count] + ',' + DATA_STAFF['name'][count] + ',' \
+ DATA_STAFF['age'][count] + ',' + DATA_STAFF['phone'][count] + ',' \
+ DATA_STAFF['depart'][count] + ',' + DATA_STAFF['enrolled_date'][count]
staff_infofile.write(staff_wr)
count += 1
if count == len(DATA_STAFF['id']):
break
staff_infofile.close()
else:
for index,name in enumerate(DATA_STAFF['name']):
if name.strip() == eval(before_update_content).strip():
DATA_STAFF['age'][index] = eval(after_update_content)
else:
pass
count = 0
staff_infofile = open('staff_info.txt', 'w', encoding='utf_8')
while True:
staff_wr = str(DATA_STAFF['id'][count]) + ',' + DATA_STAFF['name'][count] + ',' \
+ str(DATA_STAFF['age'][count]) + ',' + str(DATA_STAFF['phone'][count]) + ',' \
+ DATA_STAFF['depart'][count] + ',' + DATA_STAFF['enrolled_date'][count]
staff_infofile.write(staff_wr)
count += 1
if count == len(DATA_STAFF['id']):
break
staff_infofile.close()
elif update_staff == 'q':
break
else:
print("\033[31;1m輸入的文法有誤,請重輸!\33[0m")
update_func()
def main():
while True:
print(prompt_func())
user_input = input("請輸入要執行操作的序号>> ")
user_actions = {
1: find_func,
2: add_func,
3: del_func,
4: update_func,
}
if user_input.isdigit():
if int(user_input) in user_actions.keys():
user_action = user_actions[int(user_input)]
user_action()
else:
print('\033[1;31m 輸入錯誤,請重新輸入 \033[0m')
continue
else:
print('\033[1;31m 輸入錯誤,請重新輸入 \033[0m')
continue
if __name__ == '__main__':
main()
四,測試
4.1 主界面如下:
歡迎來到員工資訊查詢系統!
操作選項:
1、模糊查詢員工資訊
2、新增員工資訊
3、删除指定員工資訊
4、修改員工資訊
5、quit傳回上一級
請輸入要執行操作的序号>>
4.2 模糊查詢員工資訊界面:
請輸入要執行操作的序号>> 1
-------------歡迎進入模糊查詢員工資訊界面----------------
***************************************************
指令行示例:
find name age where age > 20
find * from staff_table where dept IT
find * from staff_table where enroll_date like 2013
***************************************************
請輸入您要查詢員工資訊的正确的文法(如果想傳回上一層,請按q):
4.3 新增員工資訊界面:
請輸入要執行操作的序号>> 2
-------------歡迎進入新增員工資訊界面----------------
***************************************************************
員工錄入示例:add staff_table Alex Li,25,134435344,IT,2015-10-29
***************************************************************
請輸入您要增加員工資訊的正确的文法(如果想傳回上一層,請按q):
4.4 删除指定員工資訊界面:
請輸入要執行操作的序号>> 3
-------------歡迎進入删除指定員工資訊界面----------------
***********************************
删除示例:del from staff where id=3
***********************************
請輸入您要删除的員工id的文法:
4.5 修改員工資訊界面:
請輸入要執行操作的序号>> 4
-------------歡迎進入修改員工資訊界面----------------
*************************************************************
示例:UPDATE staff_table SET dept="Market" WHERE dept = "IT"
UPDATE staff_table SET age=25 WHERE name = "Alex Li"
*************************************************************
請輸入您要更新的員工資訊的文法:
不經一番徹骨寒 怎得梅花撲鼻香