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") #Pwiwyrfovu新建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)#清除位置 http://www.cppcns.com #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 serchPwiwyrfovu_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
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
精彩评论