您好,登錄后才能下訂單哦!
這篇“python如何實現(xiàn)簡易圖書管理系統(tǒng)”文章的知識點大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細,步驟清晰,具有一定的借鑒價值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“python如何實現(xiàn)簡易圖書管理系統(tǒng)”文章吧。
1.添加書籍
2.查詢數(shù)據(jù)
3.借書
存儲方式 ,用excel保存到硬盤上或者用.txt文件保存
1.用excel存儲
# 一、介紹 # 主要功能實現(xiàn) # 1、借書 # 2、添加新書 # 3、查找圖書 # 數(shù)據(jù)存儲:excel表 import xlwt import xlrd import xlutils.copy import os #book = {"位置":"","書名":"","價格":"","作者":""} #存儲方式 用excel title =["位置","書名","價格","作者"] #查看當前的書本數(shù),也就行號 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是否存在,如果不存在,就創(chuàng)建 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") #并復制之前的已經(jīng)存在的數(shù)據(jù) 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) #存儲到硬盤(將輸入的數(shù)據(jù)存儲到excel中) for i in range(4): #寫入第book_num行數(shù)據(jù) sheet1.write(book_num,i,book[i]) book_num = book_num +1 #總書數(shù)量加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("查詢成功,查詢結(jié)果為\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) #重新創(chuàng)建一個excel,用于保存更新后的數(shù)據(jù) # 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("******圖書管理系統(tǒng)****") 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("圖書管理系統(tǒng).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("圖書管理系統(tǒng).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("查詢到的數(shù)據(jù)信息為:",read_data) break else: print("查找失敗") file.close() return read_data def borrow_book(): file = open("圖書管理系統(tǒng).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("圖書管理系統(tǒng).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直接打開一個文件,如果文件不存在則創(chuàng)建文件 while(1): print("******圖書管理系統(tǒng)****") 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
以上就是關(guān)于“python如何實現(xiàn)簡易圖書管理系統(tǒng)”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對大家有幫助,若想了解更多相關(guān)的知識內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。