In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
The knowledge of this article "python how to achieve a simple library management system" is not understood by most people, so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "python how to achieve a simple library management system" article.
I. Design requirements
1. Add a book
two。 Query data
3. Borrow books
Storage mode, save to hard disk with excel or save with .txt file
Second, implementation code
1. Use excel to store
# 1. Introduce # main functions # 1, borrow books # 2, add new books # 3, find books # data storage: excel table import xlwtimport xlrdimport xlutils.copyimport os#book = {"location", "title": "," Price "," author ":"} # Storage method: exceltitle = [location "," title "," Price "," author "] # View the current number of books Line number def read_book_num (): path = os.path.join (os.getcwd () + r'\ Books .xls') print (path) flag = os.path.exists (path) if (flag): book_excel = xlrd.open_workbook ("Books .xls") sheet1 = book_excel.sheets () [0] book_num = sheet1.nrows else: book_num = 0 return book_numdef add_book (book_num): # determine whether excel exists If it doesn't exist, Just create path = os.path.join (os.getcwd () + r'\ Books .xls') flag = os.path.exists (path) print ("flag", flag) if (flag): # if it exists Just open excel book_excel = xlrd.open_workbook ("Book .xls") # and copy the existing data book_excel = xlutils.copy.copy (book_excel) sheet1 = book_excel.get_sheet (0) # sheet1 = book_excel.sheets () [0] else: book_excel = xlwt.Workbook ("Book .xls") # New Build excel sheet1 = book_excel.add_sheet (sheetname= "Book form") Cell_overwrite_ok=True) while (1): # print prompt button_num = input ("Please select your operation\ n:" + "1. Add new book\ n "+" 2. To exit, press Q\ n ") if (button_num = ='q'): break elif (button_num = =" 1 "): # enter all the information about a book And first store it in book book = [] # empty book information input_value =''# empty input for i in range (4): print ("Please enter:" Title [I]) input_value = input () book.append (input_value) # Storage to hard disk (store input data in excel) for i in range (4): # write to the book _ num line data sheet1.write (book_num,i Book [I]) book_num = book_num + 1 # Total number of books plus 1 book_excel.save ("Books .xls") print ("added successfully") else: print ("invalid input" Please re-enter! ") Def search_book (): # Open excel book_excel = xlrd.open_workbook ("Books .xls") sheet1 = book_excel.sheets () [0] book_num = sheet1.nrows while (1): # enter the title of the book chose= input ("Please enter your action:\ n" + "1. Query books:\ n "+" 2. To exit, please press Q\ n ") if (chose = ='q'): break elif (chose = ='1'): bookname = input (" Please enter the title of the book: ") for i in range (0bookbookkeeper num): if (bookname = = sheet1.cell (iMagn0) .value): print (" query succeeded " The query result is\ n ", sheet1.row_values (I)) return else: print (" query failed, there is no such book in this library ") return else: print (" incorrect operation, please re-enter! ") Def borrow_book (): # Open excel book_excel = xlrd.open_workbook ("Books .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) # re-create an excel Used to save updated data # book_excel_new = xlwt.Workbook ("Book .xls") # New excel # sheet1_new = book_excel_new.add_sheet (sheetname= "1", cell_overwrite_ok=True) while (1): # enter the title of the book print ("1. Please enter the title of the borrowed book\ n2. Press Q to exit the book borrowing interface ") bookname = input () if (bookname = ='q'): return else: # find 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 J) .value) # clear location a + = 1 print ("borrowed successfully") book_excel_copy.save ('book .xls') return # else: # a = I # sheet1_copy.write (I JJM sheet1.cell (a) J) .value) # clear location # book_excel_copy.save ('book .xls') if _ _ name__ ='_ _ main__': book_num = read_book_num () print (book_num) while (1): print ("* library management system *) print (" * 1. Add a new book * ") print (" * 2. Query books * ") print (" * 3. Borrow books * ") print (" * 4. Exit * ") op = input (" Please enter your operation: ") if (op = =" 1 "): add_book (book_num) elif (op = =" 2 "): search_book () elif (op = =" 3 "): borrow_book () elif (op = =" 4 "): Break else: print ("invalid input Please re-enter! ")
two。 Store in txt file mode
Def add_book (): file = open ("Library Management system .txt", "a +") print ("Please enter the book information to add:") id = input ("id:") name = input ("bookname:") author = input ("author:") # table = [name,id,author] file.write (id+ "" + name+ "" + author+ "\ r") print ("Book added successfully!") File.close () def serch_book (): file = open ("Library Management system .txt" "r") name = input ("Please enter the title of the book to be queried:") read_data_all = [] count = len (file.readlines ()) # print (count) file.seek (0P0) # move the file pointer to the beginning 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 ("the data information queried is:" Read_data) break else: print ("failed to find") file.close () return read_datadef borrow_book (): file = open ("Library Management system .txt", "r +") # find out whether the book exists first If it exists, lend count = len (file.readlines ()) read_data_all= [] file.seek (0Let0) # you need to move the file pointer to the beginning 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 ("Library Management system .txt" "w") for line in read_data_all: if book==line: continue line_to_str =''.join (line) # convert the list into a string file.write (line_to_str+ "\ n") if _ _ name__ = = "_ _ main__": # open directly open a file If the file does not exist, create the file while (1): print ("* Library Management system *") print ("* 1. Add a new book * ") print (" * 2. Query books * ") print (" * 3. Borrow books * ") print (" * 4. Exit * ") op = input (" Please enter your operation: ") if (op = =" 1 "): add_book () elif (op = =" 2 "): serch_book () elif (op = =" 3 "): borrow_book () else: break or above It is about the content of the article "how to implement a simple Library Management system in python". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.