python實現簡易圖書管理系統
本文實例為大傢分享瞭python實現簡易圖書管理系統的具體代碼,供大傢參考,具體內容如下
一、設計需求
1.添加書籍
2.查詢數據
3.借書
存儲方式 ,用excel保存到硬盤上或者用.txt文件保存
二、實現代碼
1.用excel存儲
# 一、介紹 # 主要功能實現 # 1、借書 # 2、添加新書 # 3、查找圖書 # 數據存儲:excel表 import xlwt import xlrd import xlutils.copy import os #book = {"位置":"","書名":"","價格":"","作者":""} #存儲方式 用excel title =["位置","書名","價格","作者"] #查看當前的書本數,也就行號 def read_book_num(): path = os.path.join(os.getcwd()+r'\圖書.xls') print(path) flag = os.path.exists(path) if(flag): book_excel = xlrd.open_workbook("圖書.xls") sheet1 = book_excel.sheets()[0] book_num = sheet1.nrows else: book_num = 0 return book_num def add_book(book_num): #判斷excel是否存在,如果不存在,就創建 path = os.path.join(os.getcwd()+r'\圖書.xls') flag = os.path.exists(path) print("flag",flag) if(flag): #如果存在,就打開excel book_excel = xlrd.open_workbook("圖書.xls") #並復制之前的已經存在的數據 book_excel = xlutils.copy.copy(book_excel) sheet1 = book_excel.get_sheet(0) #sheet1 = book_excel.sheets()[0] else: book_excel = xlwt.Workbook("圖書.xls") #新建excel sheet1 = book_excel.add_sheet(sheetname="圖書表單",cell_overwrite_ok=True) while(1): #打印提示 button_num = input("請選擇你的操作\n:"+"1.添加新書\n"+"2.退出請按q\n") if(button_num == 'q'): break elif (button_num == "1"): #輸入一本書的所有信息,並且先存儲到book裡面 book = [] #清空書本信息 input_value = '' #清空輸入 for i in range(4): print("請輸入:",title[i]) input_value = input() book.append(input_value) #存儲到硬盤(將輸入的數據存儲到excel中) for i in range(4): #寫入第book_num行數據 sheet1.write(book_num,i,book[i]) book_num = book_num +1 #總書數量加1 book_excel.save("圖書.xls") print("添加成功") else: print("輸入無效,請重新輸入!") def search_book(): #打開excel book_excel = xlrd.open_workbook("圖書.xls") sheet1 = book_excel.sheets()[0] book_num = sheet1.nrows while(1): #輸入書名 chose= input("請輸入你的操作:\n"+"1.查詢書籍:\n"+"2.退出請按q\n") if(chose == 'q'): break elif (chose == '1'): bookname = input("請輸入書名:") for i in range(0,book_num): if(bookname == sheet1.cell(i,0).value): print("查詢成功,查詢結果為\n",sheet1.row_values(i)) return else: print("查詢失敗,本書庫沒有此書") return else: print("操作有誤,請重新輸入!") def borrow_book(): #打開excel book_excel = xlrd.open_workbook("圖書.xls") sheet1 = book_excel.sheets()[0] book_num = sheet1.nrows book_excel_copy = xlutils.copy.copy(book_excel) sheet1_copy = book_excel_copy.get_sheet(0) #重新創建一個excel,用於保存更新後的數據 # book_excel_new = xlwt.Workbook("圖書.xls") #新建excel # sheet1_new = book_excel_new.add_sheet(sheetname="1",cell_overwrite_ok=True) while(1): #輸入書名 print("1.請輸入借書書名\n2.按q退出借書界面") bookname = input() if(bookname == 'q'): return else: #查找 a = 0 for i in range(0, book_num): if( bookname == sheet1.cell(i, 0).value ): for j in range(4): a = i + 1 while(book_num-a): sheet1_copy.write(i,j,sheet1.cell(a,j).value)#清除位置 a += 1 print("借閱成功") book_excel_copy.save('圖書.xls') return # else: # a = i # sheet1_copy.write(i,j,sheet1.cell(a,j).value)#清除位置 #book_excel_copy.save('圖書.xls') if __name__ == '__main__': book_num = read_book_num() print(book_num) while(1): print("******圖書管理系統****") print("******1.添加新書******") print("******2.查詢書籍******") print("******3.借書*********") print("******4.退出*********") op = input("請輸入你的操作:") if(op == "1"): add_book(book_num) elif (op == "2"): search_book() elif (op == "3"): borrow_book() elif (op == "4"): break else: print("輸入無效,請重新輸入!")
2.用txt文件方式存儲
def add_book(): file = open("圖書管理系統.txt","a+") print("請輸入要添加的書籍信息:") id = input("id:") name = input("bookname:") author = input("author:") #table = [name,id,author] file.write(id+" "+name+" "+author+"\r") print("書籍添加成功!") file.close() def serch_book(): file = open("圖書管理系統.txt","r") name = input("請輸入要查詢書籍名稱:") read_data_all = [] count = len(file.readlines()) #print(count) file.seek(0,0) #需要將文件指針移動到開頭 for i in range(count): read_data = file.readline().split() read_data_all.append(read_data) for read_data in read_data_all: # print(type(read_data)) if(name==read_data[0]): print("查詢到的數據信息為:",read_data) break else: print("查找失敗") file.close() return read_data def borrow_book(): file = open("圖書管理系統.txt","r+") #先查找書籍存不存在,如果存在就借出 count = len(file.readlines()) read_data_all= [] file.seek(0,0) #需要將文件指針移動到開頭 for i in range(count): read_data = file.readline().split() read_data_all.append(read_data) print(read_data_all) file.close() book = serch_book() file = open("圖書管理系統.txt","w") for line in read_data_all: if book==line: continue line_to_str = ' '.join(line) #將列表裝換成字符串 file.write(line_to_str+"\n") if __name__ == "__main__": #open直接打開一個文件,如果文件不存在則創建文件 while(1): print("******圖書管理系統****") print("******1.添加新書******") print("******2.查詢書籍******") print("******3.借書**********") print("******4.退出**********") op = input("請輸入你的操作:") if(op == "1"): add_book() elif(op == "2"): serch_book() elif(op == "3"): borrow_book() else: break
以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。
推薦閱讀:
- python 使用xlsxwriter循環向excel中插入數據和圖片的操作
- 使用Python封裝excel操作指南
- python技巧分享Excel創建和修改
- Python自動化之批量處理工作簿和工作表
- python之openpyxl模塊的安裝和基本用法(excel管理)