README:
1. 员工信息表程序,实现增删改查操作:
1).可进行模糊查询,语法至少支持下面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"
最后显示有查到的条数
2).可创建新员工纪录,以phone做唯一键,staff_id需自增
3).可删除指定员工信息纪录,输入员工id,即可删除
4).可修改员工信息,语法如下:
UPDATE staff_table SET dept="Market" WHERE dept = "IT"
流程图:
代码:
# coding:utf8import sysimport redef select(staff, field): cmd = input("cmd>").strip() cmd = cmd.replace('FROM', 'from') cmd = cmd.replace('WHERE', 'where')if '*' in cmd:for i in field.keys(): sys.stdout.write(str(i) + ' ')print('')for line in staff: info_list = re.split(r',+', line.strip('\n')) cmd_list = re.split(r'[ ,;]+', cmd) f_index = cmd_list.index('from') search_field = cmd_list[1:f_index] from_field = cmd_list[f_index + 1]if from_field != 'staff_table':print('\033[31;1mplease select `staff_table`...\033[0m')breakif 'where' not in cmd_list:# 不存在where条件,显示所有view_list = []for i in range(len(search_field)):if search_field[i] == '*': view_list = info_list[:]else: view_list.append(info_list[field.get(search_field[i])])else:print(','.join(view_list))else:# 存在where条件w_index = cmd_list.index('where') where_str = ''.join(cmd_list[w_index + 1:])if re.search(r'like', where_str): sizeof = 'like'where_list = re.split(r'like', where_str)else: sizeof = re.search(r'[=>, ': v = info_list[field.get(where_field)]if float(v) > value: view_list = []for i in range(len(search_field)):if search_field[i] == '*': view_list = info_list[:]else: view_list.append(info_list[field.get(search_field[i])])else:print(','.join(view_list))elif sizeof == '").strip() up_list = [] cmd = cmd.replace('SET', 'set') cmd = cmd.replace('WHERE', 'where')for line in staff: info_list = re.split(r',+', line.strip('\n')) cmd_list = re.split(r'[ ,;]+', cmd)if cmd_list[1] != 'staff_table':print('\033[31;1mplease update `staff_table`...\033[0m')breakset_index = cmd_list.index('set') where_index = cmd_list.index('where') set_list = re.split(r'=', ''.join(cmd_list[set_index + 1:where_index])) set_field = set_list[0] set_value = set_list[1]if re.search(r'[\'\"]+', set_value): set_value = set_value.replace('\'', '') set_value = set_value.replace('\"', '') where_list = re.split(r'=', ''.join(cmd_list[where_index + 1:])) where_field = where_list[0] where_value = where_list[1]if re.search(r'[\'\"]+', where_value): where_value = where_value.replace('\'', '') where_value = where_value.replace('\"', '')if info_list[field.get(where_field)] == where_value: info_list[field.get(set_field)] = set_valueprint(','.join(info_list)) up_list.append(','.join(info_list))else:return up_listdef delete(staff): del_id = input("delete id:").strip()for i in range(len(staff)):if re.split(r',', staff[i])[0] == del_id: staff.pop(i)print("id:%s delete success." % del_id)return staffelse:return Falsedef main(): staff_table = 'staff_table.txt'field = {'staff_id': 0,'name': 1,'age': 2,'phone': 3,'dept': 4,'enroll_date': 5} menu = '''\033[33;1m-- staff_table --\033[0m\033[29;1m S. 查询 h. 帮助 A. 新增 q. 退出 E. 修改 D. 删除\033[0m'''print(menu)while True:try: with open(staff_table, 'r') as f: staff = f.readlines() choice = input(">>").strip().lower()if choice == 'h': # helpprint(menu)elif choice == 'q':breakelif choice == 's': # search select(staff, field)elif choice == 'a': # addresult = add(staff)if not result:print('\033[31;1madd failed...\033[0m')continueelse: with open(staff_table, 'w') as f: f.writelines(result)elif choice == 'e': # updateresult = update(staff, field)if not result:print('\033[31;1mupdate failed...\033[0m')continueelse: with open(staff_table, 'w') as f:for line in result: f.write(line + '\n')elif choice == 'd': # deleteresult = delete(staff)if not result:print('\033[31;1mdelete failed...\033[0m')continueelse: with open(staff_table, 'w') as f: f.writelines(result)else:continueexcept:print("\033[31;1minput error...\033[0m")continueif __name__ == '__main__': main()
View Code
以上是實現員工資訊表展示功能的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Python和C 各有優勢,選擇應基於項目需求。 1)Python適合快速開發和數據處理,因其簡潔語法和動態類型。 2)C 適用於高性能和系統編程,因其靜態類型和手動內存管理。

選擇Python還是C 取決於項目需求:1)如果需要快速開發、數據處理和原型設計,選擇Python;2)如果需要高性能、低延遲和接近硬件的控制,選擇C 。

通過每天投入2小時的Python學習,可以有效提升編程技能。 1.學習新知識:閱讀文檔或觀看教程。 2.實踐:編寫代碼和完成練習。 3.複習:鞏固所學內容。 4.項目實踐:應用所學於實際項目中。這樣的結構化學習計劃能幫助你係統掌握Python並實現職業目標。

在兩小時內高效學習Python的方法包括:1.回顧基礎知識,確保熟悉Python的安裝和基本語法;2.理解Python的核心概念,如變量、列表、函數等;3.通過使用示例掌握基本和高級用法;4.學習常見錯誤與調試技巧;5.應用性能優化與最佳實踐,如使用列表推導式和遵循PEP8風格指南。

Python適合初學者和數據科學,C 適用於系統編程和遊戲開發。 1.Python簡潔易用,適用於數據科學和Web開發。 2.C 提供高性能和控制力,適用於遊戲開發和系統編程。選擇應基於項目需求和個人興趣。

Python更適合數據科學和快速開發,C 更適合高性能和系統編程。 1.Python語法簡潔,易於學習,適用於數據處理和科學計算。 2.C 語法複雜,但性能優越,常用於遊戲開發和系統編程。

每天投入兩小時學習Python是可行的。 1.學習新知識:用一小時學習新概念,如列表和字典。 2.實踐和練習:用一小時進行編程練習,如編寫小程序。通過合理規劃和堅持不懈,你可以在短時間內掌握Python的核心概念。

Python更易學且易用,C 則更強大但複雜。 1.Python語法簡潔,適合初學者,動態類型和自動內存管理使其易用,但可能導致運行時錯誤。 2.C 提供低級控制和高級特性,適合高性能應用,但學習門檻高,需手動管理內存和類型安全。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

禪工作室 13.0.1
強大的PHP整合開發環境