您的位置:首頁技術文章
文章詳情頁

用Python實現職工信息管理系統

瀏覽:2日期:2022-06-30 18:27:42

想要實現一個職工管理系統首先我們看一下想要實現什么功能最基礎的增刪改查肯定要實現的然后增加一下數據顯示、數據排序、數據統計功能

下面直接上代碼

增加職工數據

# 接收用戶收入id = input(’請輸入職工號’)name = input(’請輸入姓名’)sex = input(’請輸入性別’)age = input(’請輸入年齡’)education = input(’請輸入學歷’)address = input(’請輸入住址’)photonumber = input(’請輸入電話’)money = input(’請輸入工資’)# 向列表中添加數據data.append([id, name, sex, age, education, address, photonumber, money])print(’添加成功’)# 調用保存函數 保存數據save()

刪除職工數據

id = input(’請輸入你要修改的職工編號’)ids = [i[0] for i in data]if id not in ids: print(’您查詢的職工不存在’) returnelse: del data[ids.index(id)] print(’刪除成功’)save()

查詢職工數據

# 選擇查詢目標flag = int(input(’1.按照職工編號查詢 2.按照職工姓名查詢’))if flag == 1: id = input(’輸入職工編號’) # 職工編號列表 ids = [i[0] for i in data] # 判斷輸入的編號是否存在 if id not in ids: print(’您查詢的職工不存在’) return else: print(’職工號 姓名 性別 年齡 學歷 住址 電話 工資’) # 打印該編號的信息 for i in data[ids.index(id)]: print(i, end=’ ’) print()else: name = input(’輸入職工姓名’) # 職工姓名列表 names = [i[1] for i in data] # 判斷輸入的姓名是否存在 if name not in names: print(’您查詢的職工不存在’) return else: print(’職工號 姓名 性別 年齡 學歷 住址 電話 工資’) # 同上 for i in data[names.index(name)]: print(i, end=’ ’) print()

修改職工信息

id = input(’請輸入你要修改的職工編號’)ids = [i[0] for i in data]if id not in ids: print(’您查詢的職工不存在’) returnelse: # 輸入要修改的數據 name = input(’請輸入姓名’) sex = input(’請輸入性別’) age = input(’請輸入年齡’) education = input(’請輸入學歷’) address = input(’請輸入住址’) photonumber = input(’請輸入電話’) money = input(’請輸入工資’) # 修改數據 data[ids.index(id)] = [id, name, sex, age, education, address, photonumber, money] print(’修改成功’)save()

排序函數

global datadata = sorted(data, key=lambda x: x[1])

統計函數

counts = {}# 統計每個工資的人數for i in data: counts[int(i[-1])] = counts.get(i[-1], 0) + 1# 按照人數多少排序counts = dict(sorted(counts.items(), key=lambda x: x[1], reverse=True))# 將結果打印for money, count in counts.items(): print(’{0:<10}{1:>5}’.format(money, count))print(’工資最多的是:’, max(counts))print(’工資最少的是:’, min(counts))

顯示函數

# 打印標題print(’職工號 姓名 性別 年齡 學歷 住址 電話 工資’)# 遍歷數據列表 然后打印數據for i in data: for j in i: print(j, end=’ ’) print()

讀取保存函數

def save(): # 保存函數 # 打開文件,寫入數據 with open(’數據.csv’,’w’) as j: for i in data: j.write(’,’.join(i)+’n’) j.close()def load(): # 讀取函數 # 讀取文件 with open(’數據.csv’,’r’) as j: # 讀取每行數據 for i in j.readlines(): # 清洗掉換行符 然后以逗號為間隔符分割 data.append(i.replace(’n’,’’).split(’,’)) j.close()

總結整體代碼:

def add(): # 添加數據函數 # 接收用戶收入 id = input(’請輸入職工號’) name = input(’請輸入姓名’) sex = input(’請輸入性別’) age = input(’請輸入年齡’) education = input(’請輸入學歷’) address = input(’請輸入住址’) photonumber = input(’請輸入電話’) money = input(’請輸入工資’) # 向列表中添加數據 data.append([id, name, sex, age, education, address, photonumber, money]) print(’添加成功’) # 調用保存函數 保存數據 save()def show(): # 顯示函數 # 打印標題 print(’職工號 姓名 性別 年齡 學歷 住址 電話 工資’) # 遍歷數據列表 然后打印數據 for i in data: for j in i: print(j, end=’ ’) print()def quety(): # 查詢函數 # 選擇查詢目標 flag = int(input(’1.按照職工編號查詢 2.按照職工姓名查詢’)) if flag == 1: id = input(’輸入職工編號’) # 職工編號列表 ids = [i[0] for i in data] # 判斷輸入的編號是否存在 if id not in ids: print(’您查詢的職工不存在’) return else: print(’職工號 姓名 性別 年齡 學歷 住址 電話 工資’) # 打印該編號的信息 for i in data[ids.index(id)]:print(i, end=’ ’) print() else: name = input(’輸入職工姓名’) # 職工姓名列表 names = [i[1] for i in data] # 判斷輸入的姓名是否存在 if name not in names: print(’您查詢的職工不存在’) return else: print(’職工號 姓名 性別 年齡 學歷 住址 電話 工資’) # 同上 for i in data[names.index(name)]:print(i, end=’ ’) print()def modify(): # 修改函數 # 原理同上 id = input(’請輸入你要修改的職工編號’) ids = [i[0] for i in data] if id not in ids: print(’您查詢的職工不存在’) return else: # 輸入要修改的數據 name = input(’請輸入姓名’) sex = input(’請輸入性別’) age = input(’請輸入年齡’) education = input(’請輸入學歷’) address = input(’請輸入住址’) photonumber = input(’請輸入電話’) money = input(’請輸入工資’) # 修改數據 data[ids.index(id)] = [id, name, sex, age, education, address, photonumber, money] print(’修改成功’) save()def sort(): # 排序函數 global data data = sorted(data, key=lambda x: x[1])def statistics(): # 統計函數 counts = {} # 統計每個工資的人數 for i in data: counts[int(i[-1])] = counts.get(i[-1], 0) + 1 # 按照人數多少排序 counts = dict(sorted(counts.items(), key=lambda x: x[1], reverse=True)) # 將結果打印 for money, count in counts.items(): print(’{0:<10}{1:>5}’.format(money, count)) print(’工資最多的是:’, max(counts)) print(’工資最少的是:’, min(counts))def delete(): # 刪除函數 # 原理同上 id = input(’請輸入你要修改的職工編號’) ids = [i[0] for i in data] if id not in ids: print(’您查詢的職工不存在’) return else: del data[ids.index(id)] print(’刪除成功’) save()def save(): # 保存函數 # 打開文件,寫入數據 with open(’數據.csv’,’w’) as j: for i in data: j.write(’,’.join(i)+’n’) j.close()def load(): # 讀取函數 # 讀取文件 with open(’數據.csv’,’r’) as j: # 讀取每行數據 for i in j.readlines(): # 清洗掉換行符 然后以逗號為間隔符分割 data.append(i.replace(’n’,’’).split(’,’)) j.close()if __name__ == ’__main__’: data = [] # 數據保存列表 # 讀取文件 如果文件不存在 報錯跳過 無視 try: load() except FileNotFoundError: pass while True: # 根據玩家的輸入 選擇相應的功能 choice = int(input(’1.添加職工數據n2.顯示職工數據n3.查詢職工數據n4.修改職工數據n5.刪除職工數據n6.保存職工數據n7.排序職工數據n8.統計職工工資數據n9.退出’)) if choice == 1: add() elif choice == 2: show() elif choice == 3: quety() elif choice == 4: modify() elif choice == 5: delete() elif choice == 6: save() elif choice == 7: sort() elif choice == 8: statistics() elif choice == 9: print(’退出程序’) break

到此這篇關于用Python實現職工信息管理系統的文章就介紹到這了,更多相關Python 職工信息管理系統內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Python 編程
相關文章:
国产综合久久一区二区三区