用Python實現(xiàn)職工信息管理系統(tǒng)
想要實現(xiàn)一個職工管理系統(tǒng)首先我們看一下想要實現(xiàn)什么功能最基礎(chǔ)的增刪改查肯定要實現(xiàn)的然后增加一下數(shù)據(jù)顯示、數(shù)據(jù)排序、數(shù)據(jù)統(tǒng)計功能
下面直接上代碼
增加職工數(shù)據(jù)
# 接收用戶收入id = input(’請輸入職工號’)name = input(’請輸入姓名’)sex = input(’請輸入性別’)age = input(’請輸入年齡’)education = input(’請輸入學(xué)歷’)address = input(’請輸入住址’)photonumber = input(’請輸入電話’)money = input(’請輸入工資’)# 向列表中添加數(shù)據(jù)data.append([id, name, sex, age, education, address, photonumber, money])print(’添加成功’)# 調(diào)用保存函數(shù) 保存數(shù)據(jù)save()
刪除職工數(shù)據(jù)
id = input(’請輸入你要修改的職工編號’)ids = [i[0] for i in data]if id not in ids: print(’您查詢的職工不存在’) returnelse: del data[ids.index(id)] print(’刪除成功’)save()
查詢職工數(shù)據(jù)
# 選擇查詢目標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(’職工號 姓名 性別 年齡 學(xué)歷 住址 電話 工資’) # 打印該編號的信息 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(’職工號 姓名 性別 年齡 學(xué)歷 住址 電話 工資’) # 同上 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: # 輸入要修改的數(shù)據(jù) name = input(’請輸入姓名’) sex = input(’請輸入性別’) age = input(’請輸入年齡’) education = input(’請輸入學(xué)歷’) address = input(’請輸入住址’) photonumber = input(’請輸入電話’) money = input(’請輸入工資’) # 修改數(shù)據(jù) data[ids.index(id)] = [id, name, sex, age, education, address, photonumber, money] print(’修改成功’)save()
排序函數(shù)
global datadata = sorted(data, key=lambda x: x[1])
統(tǒng)計函數(shù)
counts = {}# 統(tǒng)計每個工資的人數(shù)for i in data: counts[int(i[-1])] = counts.get(i[-1], 0) + 1# 按照人數(shù)多少排序counts = dict(sorted(counts.items(), key=lambda x: x[1], reverse=True))# 將結(jié)果打印for money, count in counts.items(): print(’{0:<10}{1:>5}’.format(money, count))print(’工資最多的是:’, max(counts))print(’工資最少的是:’, min(counts))
顯示函數(shù)
# 打印標題print(’職工號 姓名 性別 年齡 學(xué)歷 住址 電話 工資’)# 遍歷數(shù)據(jù)列表 然后打印數(shù)據(jù)for i in data: for j in i: print(j, end=’ ’) print()
讀取保存函數(shù)
def save(): # 保存函數(shù) # 打開文件,寫入數(shù)據(jù) with open(’數(shù)據(jù).csv’,’w’) as j: for i in data: j.write(’,’.join(i)+’n’) j.close()def load(): # 讀取函數(shù) # 讀取文件 with open(’數(shù)據(jù).csv’,’r’) as j: # 讀取每行數(shù)據(jù) for i in j.readlines(): # 清洗掉換行符 然后以逗號為間隔符分割 data.append(i.replace(’n’,’’).split(’,’)) j.close()
總結(jié)整體代碼:
def add(): # 添加數(shù)據(jù)函數(shù) # 接收用戶收入 id = input(’請輸入職工號’) name = input(’請輸入姓名’) sex = input(’請輸入性別’) age = input(’請輸入年齡’) education = input(’請輸入學(xué)歷’) address = input(’請輸入住址’) photonumber = input(’請輸入電話’) money = input(’請輸入工資’) # 向列表中添加數(shù)據(jù) data.append([id, name, sex, age, education, address, photonumber, money]) print(’添加成功’) # 調(diào)用保存函數(shù) 保存數(shù)據(jù) save()def show(): # 顯示函數(shù) # 打印標題 print(’職工號 姓名 性別 年齡 學(xué)歷 住址 電話 工資’) # 遍歷數(shù)據(jù)列表 然后打印數(shù)據(jù) for i in data: for j in i: print(j, end=’ ’) print()def quety(): # 查詢函數(shù) # 選擇查詢目標 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(’職工號 姓名 性別 年齡 學(xué)歷 住址 電話 工資’) # 打印該編號的信息 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(’職工號 姓名 性別 年齡 學(xué)歷 住址 電話 工資’) # 同上 for i in data[names.index(name)]:print(i, end=’ ’) print()def modify(): # 修改函數(shù) # 原理同上 id = input(’請輸入你要修改的職工編號’) ids = [i[0] for i in data] if id not in ids: print(’您查詢的職工不存在’) return else: # 輸入要修改的數(shù)據(jù) name = input(’請輸入姓名’) sex = input(’請輸入性別’) age = input(’請輸入年齡’) education = input(’請輸入學(xué)歷’) address = input(’請輸入住址’) photonumber = input(’請輸入電話’) money = input(’請輸入工資’) # 修改數(shù)據(jù) data[ids.index(id)] = [id, name, sex, age, education, address, photonumber, money] print(’修改成功’) save()def sort(): # 排序函數(shù) global data data = sorted(data, key=lambda x: x[1])def statistics(): # 統(tǒng)計函數(shù) counts = {} # 統(tǒng)計每個工資的人數(shù) for i in data: counts[int(i[-1])] = counts.get(i[-1], 0) + 1 # 按照人數(shù)多少排序 counts = dict(sorted(counts.items(), key=lambda x: x[1], reverse=True)) # 將結(jié)果打印 for money, count in counts.items(): print(’{0:<10}{1:>5}’.format(money, count)) print(’工資最多的是:’, max(counts)) print(’工資最少的是:’, min(counts))def delete(): # 刪除函數(shù) # 原理同上 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(): # 保存函數(shù) # 打開文件,寫入數(shù)據(jù) with open(’數(shù)據(jù).csv’,’w’) as j: for i in data: j.write(’,’.join(i)+’n’) j.close()def load(): # 讀取函數(shù) # 讀取文件 with open(’數(shù)據(jù).csv’,’r’) as j: # 讀取每行數(shù)據(jù) for i in j.readlines(): # 清洗掉換行符 然后以逗號為間隔符分割 data.append(i.replace(’n’,’’).split(’,’)) j.close()if __name__ == ’__main__’: data = [] # 數(shù)據(jù)保存列表 # 讀取文件 如果文件不存在 報錯跳過 無視 try: load() except FileNotFoundError: pass while True: # 根據(jù)玩家的輸入 選擇相應(yīng)的功能 choice = int(input(’1.添加職工數(shù)據(jù)n2.顯示職工數(shù)據(jù)n3.查詢職工數(shù)據(jù)n4.修改職工數(shù)據(jù)n5.刪除職工數(shù)據(jù)n6.保存職工數(shù)據(jù)n7.排序職工數(shù)據(jù)n8.統(tǒng)計職工工資數(shù)據(jù)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
到此這篇關(guān)于用Python實現(xiàn)職工信息管理系統(tǒng)的文章就介紹到這了,更多相關(guān)Python 職工信息管理系統(tǒng)內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章: