python实现简易图书管理系统

本文实例为大家分享了python实现简易图书管理系统的具体代码,供大家参考,具体内容如下
一、设计需求 【python实现简易图书管理系统】1.添加书籍
2.查询数据
3.借书
存储方式 ,用excel保存到硬盘上或者用.txt文件保存
二、实现代码 1.用excel存储

# 一、介绍# 主要功能实现# 1、借书# 2、添加新书# 3、查找图书# 数据存储:excel表import xlwtimport xlrdimport xlutils.copyimport os#book = {"位置":"","书名":"","价格":"","作者":""}#存储方式用exceltitle =["位置","书名","价格","作者"]#查看当前的书本数,也就行号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.nrowselse:book_num = 0return book_numdef add_book(book_num):#判断excel是否存在,如果不存在,就创建path = os.path.join(os.getcwd()+r'\图书.xls')flag = os.path.exists(path)print("flag",flag)if(flag):#如果存在,就打开excelbook_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") #新建excelsheet1 = book_excel.add_sheet(sheetname="图书表单",cell_overwrite_ok=True)while(1):#打印提示button_num = input("请选择你的操作\n:"+"1.添加新书\n"+"2.退出请按q\n")if(button_num == 'q'):breakelif (button_num == "1"):#输入一本书的所有信息,并且先存储到book里面book = []#清空书本信息input_valuehttps://www.it610.com/article/= '' #清空输入for i in range(4):print("请输入:",title[i])input_value = https://www.it610.com/article/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 #总书数量加1book_excel.save("图书.xls")print("添加成功")else:print("输入无效,请重新输入!")def search_book():#打开excelbook_excel = xlrd.open_workbook("图书.xls")sheet1 = book_excel.sheets()[0]book_num = sheet1.nrowswhile(1):#输入书名 chose= input("请输入你的操作:\n"+"1.查询书籍:\n"+"2.退出请按q\n")if(chose == 'q'):breakelif (chose == '1'):bookname = input("请输入书名:")for i in range(0,book_num):if(bookname == sheet1.cell(i,0).value):print("查询成功,查询结果为\n",sheet1.row_values(i))returnelse:print("查询失败,本书库没有此书")returnelse:print("操作有误,请重新输入!")def borrow_book():#打开excelbook_excel = xlrd.open_workbook("图书.xls")sheet1 = book_excel.sheets()[0]book_num = sheet1.nrowsbook_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 = 0for i in range(0, book_num):if( bookname == sheet1.cell(i, 0).value ):for j in range(4):a = i + 1while(book_num-a):sheet1_copy.write(i,j,sheet1.cell(a,j).value)#清除位置a += 1print("借阅成功")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"):breakelse: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 = https://www.it610.com/article/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)breakelse:print("查找失败")file.close()return read_datadef borrow_book():file = open("图书管理系统.txt","r+")#先查找书籍存不存在,如果存在就借出count = len(file.readlines())read_data_all= []file.seek(0,0) #需要将文件指针移动到开头for i in range(count):read_data = https://www.it610.com/article/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:continueline_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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

    推荐阅读