python如何实现简易图书管理系统

发布时间:2022-03-11 09:16:50 作者:iii
来源:亿速云 阅读:177

这篇“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

以上就是关于“python如何实现简易图书管理系统”这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注亿速云行业资讯频道。

推荐阅读:
  1. python实现简易ATM
  2. python怎么实现简单图书管理系统

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

python

上一篇:html中的table表格标签内容如何居中显示

下一篇:小程序日历组件如何开发

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》