網站首頁 編程語言 正文
本文實例為大家分享了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
原文鏈接:https://blog.csdn.net/qq_42585108/article/details/119806682
相關推薦
- 2022-04-12 pandas讀取csv格式數據時header參數設置方法_python
- 2022-05-18 C++中string類的常用方法實例總結_C 語言
- 2022-05-25 Inspinia的version 2.4模板使用的谷歌字體加載很慢問題解決
- 2022-03-06 Android中SurfaceFlinger工作原理_Android
- 2022-10-04 Redis分布式鎖之紅鎖的實現_Redis
- 2022-03-17 .NET?6開發TodoList應用引入第三方日志庫_實用技巧
- 2023-11-17 Python 如何獲取線程的返回值
- 2022-10-01 django中資源文件夾的引入及配置方法_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支