Python使用文件操作實現一個XX信息管理系統的示例
寫在前面
大家好,我是第一次python學了一個學期,期末要完成一個畢業生信息管理系統大作業的小韓了,由于上次沒有仔細看開發實現的要求,實現了一個簡單的畢業生信息管理系統,而這次專門整理了兩種使用文件進行保存數據實現的畢業生信息管理系統,因為是第一次學python,還不太熟悉python的寫法, 而之前是學 c 、c++,感覺我的這個寫的有一股 c/c++的內味:
1. 使用excel .xlsx 保存數據實現一個畢業生信息管理系統2. 使用文本文檔.txt保存數據實現一個畢業生信息管理系統
以下將會在代碼進行詳細的介紹
一、 對excel表格操作實現一個畢業生信息管理系統
開發要求
1. 采用 python 語言實現一個XX信息管理系統
2.實現基本的增刪改查的基本功能,還可以加上一些如排序,搜索的操作3. 采用文件保存數據(而不是每次從鍵盤輸入)
4. 各個功能界面循環調用環境以及
開發軟件
1. python 3.7.0 版本
2. pycharm 2019 中文版
一、 函數模塊設計:
1.主函數模塊
實現功能
查詢搜索畢業生信息列表 增加畢業生信息 修改畢業生信息 刪除畢業生信息 畢業生信息排序 退出畢業生信息管理系統def main(): # 主函數 arry = [0, 1, 2, 3, 4, 5] # 定義一個選項的列表 用于輸入判斷 Menu() # 先打印菜單 while 1: a = input('請輸入: ') if a.isdigit() and int(a) in arry: # 這里判斷輸入的是不是數字 且在不在選項的列表中 如果輸入合法再進行功能調用 a = int(a) while a:if a == 1: PrintStudentList() # 查詢搜索畢業生信息功能 Menu() breakif a == 2: AddStudent() # 添加畢業生信息功能 Menu() breakif a == 3: ChangeStudent() # 修改畢業生信息功能 Menu() breakif a == 4: DeleteStudent() # 刪除畢業生信息功能 Menu() breakif a == 5: SortData() # 畢業生信息排序功能 Menu() breakelif a > 5 or a < 0: print('輸入有誤!') break if a == 0: # 按0退出該畢業生信息管理系統print('系統已退出!')exit() else: print('請輸入0--5!')main()
這里因為還沒有學到python中的字典那部分知識,而pyhton中又沒有switch和case所以就使用這個 if 進行判斷 雖然比較繁瑣,但是看起來還是比較清晰易懂的
二、 數據文件設計
因為這里要采用文件進行保存數據,我第一個想到的就是excel表格,這種.xlsx文件保存數據一目了然,因此本次選擇了excel表格進行數據保存,寫入,讀取,修改,刪除等基本功能
主要信息:
本次實現的是一個畢業生信息管理系統,因此給每個畢業生設計的個人信息如下:學號 姓名 電話 年級 學院 就業 就業公司 郵箱 月薪
關于對excel 表格使用則需要導入兩個包:
from openpyxl import Workbook # 導入操作 excel時所用的庫from openpyxl import load_workbook # 用于對本地已經存在的excel文件操作
如果這里導入失敗的話,可能需要自己手動進行 openpyxl 的下載 下載方法具體如下
點擊 文件 -->> 點擊設置(setting) -->> 點擊自己的項目 -->> 點擊第一個選項 -->> 點擊頁面的右側的加號 -->> 輸入 想要導入的包 (openpyxl) -->> 點擊左下角的 install Package 稍等一會即可完成安裝
2.增加畢業生信息模塊
從鍵盤獲取輸入的信息,信息校驗成功后,先將信息保存在一個列表中,然后最后將整個列表插入到excel表格中,然后保存,這樣方便寫入,否則頻繁的打開關閉文件比較繁瑣,容易出現錯誤。例如:下面插入 學號 id
先建立一個空的列表 然后先對學號進行校驗,首先校驗學號是否合法,比如長度要求,或者插入的是否和表中的是否重復,當校驗成功后才將學號保存到 列表中
r = [] # 建立一個新的列表 在將這個列表插入到excel表中 ID = None wb = load_workbook(’StudentList.xlsx’) sheet = wb.active id = input('請輸入學號:') if CheckIdIsRight(id): while 1: id = input('請輸入正確的學號!') if not CheckIdIsRight(id):ID = idbreak else: ID = id r.append(ID) # 將輸入的ID插入到列表中
其余的其他信息依次類推最后將整個列表插入到excel表格中,然后關閉并保存文件
sheet.append(r) # 將整個列表插入到excel 表格中 即為插入一行數據 wb.close() wb.save(’StudentList.xlsx’)
3. 查詢搜索畢業生信息模塊
該功能主要實現查詢和搜索的功能 ,比如查看所有信息列表 ,按相關信息查詢畢業生信息,例如:查詢所有畢業生信息:
def PrintAll(): wb = load_workbook(’StudentList.xlsx’) # 打開現在已經有的表 sheet = wb.active # 獲取當前活躍的表 也就是當前使用的表 for row in sheet.rows: # 循環每一行 for cell in row: # 循環每一行的單元格 print(cell.value, end=' ') # 打印出每一個單元格的數據 print() print()
只需要將每一個單元格的按順序打印出來即可
例如:按學號查詢該畢業生的信息
def SelectById(): id = input('請輸入需要查詢的學號:') if id.isdigit() and not CheckIdIsRight(id): id1 = int(id) # 將輸入的str類型的轉換為 int 類型 wb = load_workbook(’StudentList.xlsx’) # 打開現在已經有的表 sheet = wb.active # 獲取當前活躍的表 也就是當前使用的表 r = FindId(id1) for i in range(1, 10):print(sheet.cell(1, i).value, end=' ') # 打印出表頭的信息 print() for i in range(1, 10):print(sheet.cell(r, i).value, end=' ') # 打印出該id對應的信息 print() else: print('學號輸入錯誤!')
首先應該判斷一下輸入的學號是不是一串數字,而且 想要查詢的學生的學號是存在的,因為我們這里規定學號應該是類似于1700000000 這樣的一個數字,而python默認 input 輸入的是一個 str 字符串類型的 所以這里防止輸入錯誤導致程序崩潰 因此加入了一些校驗,當確認合法之后 再將其 轉換為 int 類型的變量 進行使用。而具體的就是通過一個findid的函數來返回學號所在的行 這樣就可以將這一行的信息打印出來即可 ,打印學生信息的同時不要忘了打印表頭的信息,這樣看起來會更加的清晰。
4. 修改畢業生信息模塊
在修改該學生信息之前,同樣對其輸入的學號進行校驗,校驗完成之后進行相關信息的修改修改的基本方法就是,通過學號找到該學生所在的行,然后對特定的列的信息修改(直接賦值),最后保存到文件即可例如 : 修改姓名
def changename(row, wb): # 修改姓名 # row 為其所在的信息的行 wb 是表格對象 name = input('請輸入修改之后的名字:') sheet.cell(row, 2, name) wb.save(’StudentList.xlsx’)
5. 畢業生信息排序
這里排序主要使用了一個冒泡排序的算法 對數據進行排序,雖然python中是有內置的排序算發法的,但是我這里還是自己實現了一個排序(升序),排完序了之后 也可以對升序的進行一個反轉 得到一個降序的列表。 因為是對某一項的單一數據進行排序,而排序結果是要求打印出所有信息的,因此先得到一個某一項數據排好序的列表,然后將列表對應的信息進行打印即可。例如: 按學號進行排序冒泡排序:
def BubbleSort(l2): # 冒泡排序對列表中的數據進行一個升序的排列 for i in range(0, len(l2)): count = 0 for j in range(1, len(l2)-i): if int(l2[j - 1]) > int(l2[j]):temp = l2[j]l2[j] = l2[j - 1]l2[j - 1] = tempcount = count + 1 if count == 0: # 算法優點 當已經有序時就不再進行排序 return l2 return l2 # 返回排好序的 列表
按學號從小到大排序并打印學生信息
def GetAllStudentById(): # 按學號排序打印出學生信息(升序) l = [] # 建立一個空的列表 wb = load_workbook(’StudentList.xlsx’) sheet = wb.active for column in list(sheet.columns)[0]: l.append(column.value) # 將學號插入到列表中 得到一個學號列表 l2 = l[1:] # 由于第一個是表頭 將第二個位置以后的范圍 拷貝給 l2 l3 = BubbleSort(l2) # 進行 學號排序 # 3 是排好序的列表 for i in range(1, 10): print(sheet.cell(1, i).value, end=' ') # 打印出表頭的信息 print() for i in range(0, len(l3)): # 依次找到排好序的學號或年級對應的學生信息即可 r = FindId(l3[i]) # 找到該行 for j in range(1, 10): print(sheet.cell(r, j).value, end=' ') # 打印出該id對應的信息 print()
注意 :因為學號是唯一的,因此可以通過找道該行,然后通過行號進行某一行的定向信息打印,但是像年級 ,月薪信息是有可能重復的,就不能再像學號一樣查找,打印了,但是我們可以先將年級的列表排好序,然后進行一個去重,這樣,就可以將符合滿足,排好序的年級列表中的年級對應的學生,信息全部打印出來
6. 刪除畢業生信息
非常簡單,只需要將要刪除的學生的學號輸入,然后學號校驗合法且存在之后,找到對應的該行,然后將這一行的數據刪除就可以了。
def DeleteStudent(): # 刪除學生信息 PrintAll() id = input('請輸入要刪除學生的學號:') if not CheckIdIsRight(id): # 判斷學號為id的學生是否在StudentList.xlsx中 print('學號正確!') id = int(id) row = FindId(id) # 查找其所在的行 wb = load_workbook(’StudentList.xlsx’) sheet = wb.active isdelete = input('是否刪除該學生信息?輸入是或否:') if isdelete == ’是’: sheet.delete_rows(row, 1) # 刪除該行 wb.save(’StudentList.xlsx’) print('刪除成功!') else: print('刪除失敗!') else: print('學號輸入錯誤!')
三、 測試
1. 查詢搜索測試
2. 添加測試
3.修改測試
4. 信息排序測試
5. 刪除信息測試
四、 源碼
from openpyxl import Workbook # 導入操作 excel時所用的庫from openpyxl import load_workbookIsJob = [’是’, ’否’]def Menu(): # 菜單主界面 print(end=' ' * 45) print(’*’ * 22) print(end=' ' * 45) print('* 查詢畢業生信息輸入: 1 *') print(end=' ' * 45) print('* 添加畢業生信息輸入: 2 *') print(end=' ' * 45) print('* 修改畢業生信息輸入: 3 *') print(end=' ' * 45) print('* 刪除畢業生信息輸入: 4 *') print(end=' ' * 45) print('* 查看排序統計請輸入: 5 *') print(end=' ' * 45) print('* 退出系統請輸入: 0 *') print(end=' ' * 45) print(’*’ * 22)def SelectStudentMenu(): print(end=' ' * 45) print(’*’ * 25) print(end=' ' * 45) print('* 查看所有信息請輸入: 1 *') print(end=' ' * 45) print('* 按學號查詢信息輸入: 2 *') print(end=' ' * 45) print('* 按年級查詢信息輸入: 3 *') print(end=' ' * 45) print('* 按是否就業查詢輸入: 4 *') print(end=' ' * 45) print('* 退出查詢功能請輸入: 0 *') print(end=' ' * 45) print(’*’ * 25)def FindId(id): # 在excel中找到該 id 所在的行 返回行數 i = 0 wb = load_workbook(’StudentList.xlsx’) sheet = wb.active for column in list(sheet.columns)[0]: # 循環學號那一列的數據 i = i + 1 if id == column.value: # 找到了返回 return i # 返回行數def BubbleSort(l2): # 冒泡排序對列表中的數據進行一個升序的排列 for i in range(0, len(l2)): count = 0 for j in range(1, len(l2)): if int(l2[j - 1]) > int(l2[j]):temp = l2[j]l2[j] = l2[j - 1]l2[j - 1] = tempcount = count + 1 if count == 0: # 算法優點 當已經有序時就不再進行排序 return l2 return l2 # 返回排好序的 列表def GetAllStudentByGadeOrMoney(x): l = [] # 建立一個空的列表 用于存放數據進行排序 wb = load_workbook(’StudentList.xlsx’) sheet = wb.active for column in list(sheet.columns)[x]: l.append(column.value) # 將薪資或年級插入到列表中 得到一個薪資或年級列表 l2 = l[1:] # 由于第一個是表頭 將第二個位置以后的范圍 拷貝給 l2 l3 = BubbleSort(l2) # 進行 薪資或年級排序 # 3 是排好序的列表 i = 1 l3.reverse() # 進行一個反轉列表 得到一個降序的列表 while i < len(l3): # 這是為了剔除列表中相同的元素 if l3[i] == l3[i - 1]: del l3[i - 1] else: i = i + 1 for i in range(1, 10): print(sheet.cell(1, i).value, end=' ') # 打印出表頭的信息 print() j = 0 while j < len(l3): # 按照排好序的列表對應的值 在excel中查找 打印出對應的信息 for row in sheet.rows: # 循環每一行 for cell in row: # 循環每一行的單元格if cell.value == l3[j]: # 找到年級符合的學生的信息 for cell in row: print(cell.value, end=' ') # 打印出這一行的信息 print() j = j + 1 print()def GetAllStudentById(): # 按學號排序打印出學生信息(升序) l = [] # 建立一個空的列表 wb = load_workbook(’StudentList.xlsx’) sheet = wb.active for column in list(sheet.columns)[0]: l.append(column.value) # 將學號插入到列表中 得到一個學號列表 l2 = l[1:] # 由于第一個是表頭 將第二個位置以后的范圍 拷貝給 l2 l3 = BubbleSort(l2) # 進行 學號排序 # 3 是排好序的列表 for i in range(1, 10): print(sheet.cell(1, i).value, end=' ') # 打印出表頭的信息 print() for i in range(0, len(l3)): # 依次找到排好序的學號或年級對應的學生信息即可 r = FindId(l3[i]) # 找到該行 for j in range(1, 10): print(sheet.cell(r, j).value, end=' ') # 打印出該id對應的信息 print()def PrintAll(): wb = load_workbook(’StudentList.xlsx’) # 打開現在已經有的表 sheet = wb.active # 獲取當前活躍的表 也就是當前使用的表 for row in sheet.rows: # 循環每一行 for cell in row: # 循環每一行的單元格 print(cell.value, end=' ') # 打印出每一個單元格的數據 print() print()def PrintStudentList(): # 打印excel文件中的數據 def SelectById(): id = input('請輸入需要查詢的學號:') if id.isdigit() and not CheckIdIsRight(id): id1 = int(id) wb = load_workbook(’StudentList.xlsx’) # 打開現在已經有的表 sheet = wb.active # 獲取當前活躍的表 也就是當前使用的表 r = FindId(id1) for i in range(1, 10):print(sheet.cell(1, i).value, end=' ') # 打印出表頭的信息 print() for i in range(1, 10):print(sheet.cell(r, i).value, end=' ') # 打印出該id對應的信息 print() else: print('學號輸入錯誤!') def SelectByGrade(): wb = load_workbook(’StudentList.xlsx’) # 打開現在已經有的表 sheet = wb.active # 獲取當前活躍的表 也就是當前使用的表 grade = input('請輸入要查詢的年級:') if grade.isdigit(): for i in range(1, 10):print(sheet.cell(1, i).value, end=' ') # 打印出表頭的信息 print() # 這里也需要進行優化 for row in sheet.rows: # 循環每一行for cell in row: # 循環每一行的單元格 if cell.value == int(grade): # 找到年級符合的學生的信息 for cell in row: print(cell.value, end=' ') # 打印出這一行的信息 print() print() else: print('輸入不合法!') def SelectByIsJob(): wb = load_workbook(’StudentList.xlsx’) # 打開現在已經有的表 sheet = wb.active # 獲取當前活躍的表 也就是當前使用的表 isjob = input('請輸入要查詢的學生是否已經就業 :') if isjob in IsJob: # 檢查輸入是否正確 if isjob == ’是’: # 如果要查詢已經就業的學生for i in range(1, 10): print(sheet.cell(1, i).value, end=' ') # 打印出表頭的信息print()for row in sheet.rows: # 循環每一行 for cell in row: # 循環每一行的單元格 if cell.value == ’是’: # 找到就業信息是 ’是’的學生的那一行 for cell in row:print(cell.value, end=' ') # 打印出這一行的信息 print()print() else: # 查詢 ’否’ 還沒有就業的學生for i in range(1, 10): print(sheet.cell(1, i).value, end=' ') # 打印出表頭的信息print()for row in sheet.rows: # 循環每一行 for cell in row: # 循環每一行的單元格 if cell.value == ’否’: # 找到就業信息是 ’否’的學生的那一行的內容 for cell in row:print(cell.value, end=' ') # 打印出這一行的信息 print()print() else: print('輸入錯誤!') arry = [0, 1, 2, 3, 4] while 1: # 循環查找直到退出 SelectStudentMenu() a = (input('請輸入想要執行的操作:')) if a.isdigit() and int(a) in arry: a = int(a) while a:if a == 1: PrintAll() breakif a == 2: SelectById() breakif a == 3: SelectByGrade() breakif a == 4: SelectByIsJob() breakif a < 0 or a > 4: print('輸入錯誤!請重新輸入:') break if a == 0:break else: print('請輸入0--4!')def CheckIdIsRight(id1): # 檢查學號ID是否存在或格式不正確 wb = load_workbook(’StudentList.xlsx’) sheet = wb.active if id1.isdigit(): id2 = int(id1) for column in list(sheet.columns)[0]: if id2 == column.value:print('學號存在')return False if id2 < 1000000000 or id2 > 10000000000: print('學號格式不正確!') return True else: print('學號應該是數字!') return Truedef AddStudent(): # 添加學生信息模塊 r = [] # 建立一個新的列表 在將這個列表插入到excel表中 ID = None wb = load_workbook(’StudentList.xlsx’) sheet = wb.active id = input('請輸入學號:') if CheckIdIsRight(id): while 1: id = input('請輸入正確的學號!') if not CheckIdIsRight(id):ID = idbreak else: ID = id r.append(ID) # 將輸入的ID插入到列表中 name = input('請輸入你的名字:') # 添加姓名信息 r.append(name) # 將姓名插入到列表中 tell = input('請輸入你的電話號碼:') while 1: if len(tell) != 11: print('電話號碼格式不正確!請重新輸入:') tell = input() if len(tell) == 11:print('輸入成功!')break if len(tell) == 11: break r.append(tell) # 將電話號碼插入到列表中 grade = int(input('請輸入你的年級:')) # 添加年級信息 while 1: if grade < 2000: # 判斷年級是否正確范圍內 print('年級輸入不正確!請重新輸入:') grade = int(input()) if grade >= 2000:print('輸入成功!')break if grade >= 2000: break r.append(grade) # 將年級插入到列表中 institute = input('請輸入你的學院:') # 添加學院信息 r.append(institute) # 將學院信息插入到列表中 isjob = input('是否已經工作:輸入 :是或否!') # 添加是否就業信息 當其 是 ’是’時才能添加公司 while 1: if isjob in IsJob: r.append(isjob) break else: print('輸入錯誤請重新輸入:') isjob = input() if r[5] == ’是’: # 添加公司信息 company = input('請輸入你的公司名 ') r.append(company) else: company = ’無’ r.append(company) e_mail = input('請輸入你的電子郵箱:') # 添加郵箱信息 r.append(e_mail) # 將電子郵箱信息插入到列表中 if r[5] == ’是’: money = input('請輸入你的月薪:') # 添加月薪信息 r.append(money) # 只有當已經就業時才可以添加月薪信息 if r[5] == ’否’: money = 0 # 否則 月薪默認為 0 r.append(money) sheet.append(r) # 將整個列表插入到excel 表格中 即為插入一行數據 wb.close() wb.save(’StudentList.xlsx’)def StudentPersonalMsg(): # 修改信息界面選擇 print(end=' ' * 45) print(’*’ * 23) print(end=' ' * 45) print('* 修改學生姓名請輸入: 1 *') print(end=' ' * 45) print('* 修改電話號碼請輸入: 2 *') print(end=' ' * 45) print('* 修改是否就業請輸入: 3 *') print(end=' ' * 45) print('* 修改就業公司請輸入: 4 *') print(end=' ' * 45) print('* 修改郵箱信息請輸入: 5 *') print(end=' ' * 45) print('* 修改月薪信息請輸入: 6 *') print(end=' ' * 45) print('* 退出修改請輸入: 0 *') print(end=' ' * 45) print(’*’ * 23)def ChangeStudent(): # 修改學生信息模塊 def changename(row, wb): # 修改姓名 # row 為其所在的信息的行 wb 是表格對象 name = input('請輸入修改之后的名字:') sheet.cell(row, 2, name) wb.save(’StudentList.xlsx’) def changetell(row, wb): # 修改電話號碼 同樣進行信息格式校對 tell = input('請輸入修改后的電話號碼:') while 1: if len(tell) != 11:print('電話號碼格式不正確!請重新輸入:')tell = input()if len(tell) == 11: print('輸入成功!') break if len(tell) == 11:break sheet.cell(row, 3, tell) wb.save(’StudentList.xlsx’) def changeisjob(row, wb): # 修改是否就業狀態信息 IsJob = [’是’, ’否’] isjob = input('是否已經工作:輸入 :是或否!') while 1: if isjob in IsJob:sheet.cell(row, 6, isjob)wb.save(’StudentList.xlsx’)break else:print('輸入錯誤請重新輸入:')isjob = input() def changecompany(row, wb): # 修改公司信息 if sheet.cell(row, 6).value == ’是’: # 判斷是否就業 company = input('請輸入修改后的公司:') sheet.cell(row, 7, company) wb.save(’StudentList.xlsx’) else: print('請先修改是否就業:') changeisjob(row, wb) changecompany(row, wb) def changemail(row, wb): # 修改學生郵箱信息 mail = input('請輸入修改之后的郵箱:') sheet.cell(row, 8, mail) wb.save(’StudentList.xlsx’) def changemoney(row, wb): # 修改月薪信息 if sheet.cell(row, 6).value == ’是’: # 判斷是否就業 money = int(input('請輸入修改之后的月薪:')) sheet.cell(row, 9, money) wb.save(’StudentList.xlsx’) else: print('請先修改就業狀態及就業公司!') changeisjob(row, wb) changecompany(row, wb) changemoney(row, wb) PrintAll() arry = [0, 1, 2, 3, 4, 5, 6] id = input('請輸入你要修改的學生的學號:') if not CheckIdIsRight(id): # 檢驗學號是否存在 print('學號正確!') row = FindId(id) wb = load_workbook(’StudentList.xlsx’) sheet = wb.active StudentPersonalMsg() while 1: a = input('請輸入:') if a.isdigit() and int(a) in arry:a = int(a)while a > 0: if a == 1: changename(row, wb) print('修改成功!') break if a == 2: changetell(row, wb) print('修改成功!') break if a == 3: changeisjob(row, wb) print('修改成功!') break if a == 4: changecompany(row, wb) print('修改成功!') break if a == 5: changemail(row, wb) print('修改成功!') break if a == 6: changemoney(row, wb) print('修改成功!') break elif a > 6 or a < 0: print('輸入有誤!') breakif a == 0: break else:print('請輸入正確的選項0--6!')break else: print('請輸入正確的學號!')def DeleteStudent(): # 刪除學生信息 PrintAll() id = input('請輸入要刪除學生的學號:') if not CheckIdIsRight(id): # 判斷學號為id的學生是否在StudentList.xlsx中 print('學號正確!') id = int(id) row = FindId(id) # 查找其所在的行 wb = load_workbook(’StudentList.xlsx’) sheet = wb.active isdelete = input('是否刪除該學生信息?輸入是或否:') if isdelete == ’是’: sheet.delete_rows(row, 1) # 刪除該行 wb.save(’StudentList.xlsx’) print('刪除成功!') PrintAll() else: print('刪除失敗!') else: print('學號輸入錯誤!')def SortMenu(): print(end=' ' * 45) print(’*’ * 30) print(end=' ' * 45) print('* 按學號從小到大排序結果輸入: 1 *') print(end=' ' * 45) print('* 按年級從大到小排序結果輸入: 2 *') print(end=' ' * 45) print('* 按薪資從高到低排序結果輸入: 3 *') print(end=' ' * 45) print('* 退出此功能請輸入: 0 *') print(end=' ' * 45) print(’*’ * 30)def SortData(): SortMenu() arry = [0, 1, 2, 3] while 1: a = input('請輸入: ') if a.isdigit() and int(a) in arry: a = int(a) while a:if a == 1: GetAllStudentById() SortMenu() breakif a == 2: GetAllStudentByGadeOrMoney(3) SortMenu() breakif a == 3: GetAllStudentByGadeOrMoney(8) SortMenu() breakelif a > 3 or a < 0: print('輸入有誤!') break if a == 0:break else: print('請輸入正確的選項0--3')def main(): # 主函數 arry = [0, 1, 2, 3, 4, 5] Menu() # 先打印菜單 while 1: a = input('請輸入: ') if a.isdigit() and int(a) in arry: a = int(a) while a:if a == 1: PrintStudentList() Menu() breakif a == 2: AddStudent() Menu() breakif a == 3: ChangeStudent() Menu() breakif a == 4: DeleteStudent() Menu() breakif a == 5: SortData() Menu() breakelif a > 5 or a < 0: print('輸入有誤!') break if a == 0: # 按0退出進程print('系統已退出!')exit() else: print('請輸入0--5!')main()
文件:
注意:將表格excel文件放在代碼相同目錄下即可 ,否則應該在使用文件時填上絕對路徑,否則會出現文件打不開,或者找不到等錯誤,在系統運行期間應該講文件保存并關閉,否則當文件處于打開狀態時無法進行修改,插入等操作,出現錯誤。
二、 采用文本文檔保存數據實現的畢業生信息管理系統
基本思想與上述的相似,就不再這里闡述了,以下附上源碼。
源碼
StudentInfo = [’學號’, ’姓名’, ’性別’, ’畢業年級’, ’就業公司名稱’, ’電話號碼(+86)’, ’家庭住址’]def GetList(): # 將 StudentMsg.txt 中的數據 拷貝到一個列表中 fiel = open(’StudentMsg.txt’, ’r’, encoding=’utf-8’) l = [] for line in fiel: l.append(line.strip()) # 將所有的信息以c字符形式插入到列表中 return ldef PrintAllMsg(): # 打印出所有的信息 l = GetList() print(StudentInfo) count = 0 for i in range(0, len(l)): # 將列表中的所有信息 按7條一行打印 count = count + 1 print(l[i], end=' ') if count % 7 == 0: print() print()def ModifyMenu(): print(’-’ * 22) print('# 修改姓名請輸入: 1 *') print('# 修改性別請輸入: 2 *') print('# 修改畢業年級請輸入: 3 *') print('# 修改公司信息請輸入: 4 *') print('# 修改電話號碼請輸入: 5 *') print('# 修改家庭住址請輸入: 6 *') print('# 退出修改請輸入: 0 *') print(’-’ * 22)def ModifyMsg(): def ModifyName(pos): f = open(’StudentMsg.txt’, ’r+’, encoding=’utf-8’) flist = f.readlines() name = input('輸入修改之后的姓名:') name += ’n’ flist[pos + 1] = name f = open(’StudentMsg.txt’, ’w+’, encoding=’utf-8’) f.writelines(flist) f.close() print('修改成功!') def ModifySex(pos): Sex = [’男’, ’女’] f = open(’StudentMsg.txt’, ’r+’, encoding=’utf-8’) flist = f.readlines() sex = input('輸入修改之后的性別:') if sex in Sex: sex += ’n’ flist[pos + 2] = sex f = open(’StudentMsg.txt’, ’w+’, encoding=’utf-8’) f.writelines(flist) f.close() print('修改成功!') else: print('輸入錯誤!') print('修改失敗!') ModifySex(pos) def ModifyYear(pos): f = open(’StudentMsg.txt’, ’r+’, encoding=’utf-8’) flist = f.readlines() year = input('輸入修改之后的年級:') if int(year) > 2000: year += ’n’ flist[pos + 3] = year f = open(’StudentMsg.txt’, ’w+’, encoding=’utf-8’) f.writelines(flist) f.close() print('修改成功!') else: print('輸入錯誤!') print('修改失敗!') ModifyYear(pos) def Modifycompany(pos): f = open(’StudentMsg.txt’, ’r+’, encoding=’utf-8’) flist = f.readlines() company = input('輸入修改之后的公司:') company += ’n’ flist[pos + 4] = company f = open(’StudentMsg.txt’, ’w+’, encoding=’utf-8’) f.writelines(flist) f.close() print('修改成功!') def ModifyTell(pos): f = open(’StudentMsg.txt’, ’r+’, encoding=’utf-8’) flist = f.readlines() tell = input('輸入修改之后的電話號碼:') if len(tell) == 11: tell += ’n’ flist[pos + 5] = tell f = open(’StudentMsg.txt’, ’w+’, encoding=’utf-8’) f.writelines(flist) f.close() print('修改成功!') else: print('輸入錯誤!') print('修改失敗!') ModifyTell(pos) def ModifyAddress(pos): f = open(’StudentMsg.txt’, ’r+’, encoding=’utf-8’) flist = f.readlines() address = input('輸入修改之后的地址:') address += ’n’ flist[pos + 6] = address f = open(’StudentMsg.txt’, ’w+’, encoding=’utf-8’) f.writelines(flist) f.close() print('修改成功!') PrintAllMsg() id = input('請輸入你要修改的學號:') if id in IsIdRight(): l2 = GetList() pos = l2.index(id) while 1: ModifyMenu() a = int(input('請輸入: ')) while a:if a == 1: ModifyName(pos) breakif a == 2: ModifySex(pos) breakif a == 3: ModifyYear(pos) breakif a == 4: Modifycompany(pos) breakif a == 5: ModifyTell(pos) breakif a == 6: ModifyAddress(pos) break if a == 0: # 按0退出進程breakdef DelMsg(): PrintAllMsg() id = input('請輸入你要刪除的學生的Id:') if id in IsIdRight(): pos = GetList().index(id) f = open(’StudentMsg.txt’, ’r+’, encoding=’utf-8’) flist = f.readlines() for i in range(0, 7): del flist[pos] f = open(’StudentMsg.txt’, ’w+’, encoding=’utf-8’) f.writelines(flist) f.close() print('刪除成功!') PrintAllMsg() else: print('學號輸入錯誤!') DelMsg()def IsIdRight(): # 返回學號列表 l1 = GetList() l2 = [] i = 0 while i < len(l1): l2.append(l1[i]) i = i + 7 return l2def AddMsg(): # 添加信息 fiel = open(’StudentMsg.txt’, ’a’, encoding=’utf-8’) def Inputid(): # 添加學號判斷 id = (input('請輸入你的學號:')) l1 = IsIdRight() if not (int(id) > 1000 and (id in l1)): fiel.write(’n’) fiel.writelines(id) else: if int(id) < 1000:print('學號輸入錯誤!')Inputid() if id in IsIdRight():print('學號存在!')Inputid() def Inputname(): # 添加姓名判斷 name = input('請輸入你的姓名:') fiel.write(’n’) fiel.writelines(name) def InputSex(): # 添加性別判斷 sex = [’男’, ’女’] s1 = input('請輸入你的性別') if s1 in sex: fiel.write(’n’) fiel.writelines(s1) else: print('性別輸入錯誤!') InputSex() def InputGaduYear(): # 添加畢業年級判斷 year = (input('請輸入你的畢業年級:')) if int(year) > 2000: fiel.write(’n’) fiel.writelines(year) else: print('畢業年級輸入錯誤!') InputGaduYear() def InputCompany(): # 添加公司信息 company = input('請輸入你的就業公司:') fiel.write(’n’) fiel.writelines(company) def InputTell(): # 添加電話判斷 tell = (input('請輸入你的電話號碼:')) if len(tell) == 11: fiel.write(’n’) fiel.writelines(tell) else: print('電話號碼輸入錯誤!') InputTell() def InputAddress(): # 添加地址信息 add = input('請輸入你的家庭地址:') fiel.write(’n’) fiel.writelines(add) Inputid() Inputname() InputSex() InputGaduYear() InputCompany() InputTell() InputAddress() fiel.close() # 關閉文件def Menu(): # 菜單主界面 print(’-’ * 22) print('# 查看畢業生列表輸入: 1 *') print('# 添加畢業生信息輸入: 2 *') print('# 修改畢業生信息輸入: 3 *') print('# 查找畢業生信息輸入:4 *') print('# 刪除畢業生信息輸入: 5 *') print('# 退出系統請輸入 0 *') print(’-’ * 22)def FindMenu(): print(’-’ * 22) print('# 搜索學號請輸入: 1 *') print('# 搜索姓名請輸入: 2 *') print('# 退出搜所請輸入 0 *') print(’-’ * 22)def FindStu(): def FindMsgById(): id = input('請輸入你需要查找的學生的學號:') if id in IsIdRight(): pos = GetList().index(id) flist = GetList() print(StudentInfo) for i in range(0, 7):print(flist[pos + i], end=' ') print() else: print('學號輸入錯誤!') FindMsgById() def FindMsgByName(): name = input('請輸入你需要查找的學生的姓名:') if name in GetList(): pos = GetList().index(name) - 1 flist = GetList() print(StudentInfo) for i in range(0, 7):print(flist[pos + i], end=' ') print() else: print('姓名輸入錯誤!') FindMsgByName() while 1: FindMenu() a = int(input('請輸入: ')) while a: if a == 1:FindMsgById()break if a == 2:FindMsgByName()break if a == 0: breakdef main(): Menu() while 1: a = int(input('請輸入: ')) while a: if a == 1:PrintAllMsg()Menu()break if a == 2:AddMsg()Menu()break if a == 3:ModifyMsg()Menu()break if a == 4:FindStu()Menu()break if a == 5:DelMsg()Menu()break if a == 0: # 按0退出進程 exit()main()
相應的簡要測試:
相應的文件:注意 該文本文件應和代碼文件在同一個目錄下
注意 : 這里采用分行進行數據的存儲,為了方便數據的準確修改,不需要一整行的數據進行修改那么麻煩,插入和修改也更為精確
到此這篇關于Python使用文件操作實現一個XX信息管理系統的示例的文章就介紹到這了,更多相關Python XX信息管理系統內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!
相關文章: