基于python實(shí)現(xiàn)圖書管理系統(tǒng)
本文實(shí)例為大家分享了python實(shí)現(xiàn)圖書管理系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下
添加新書
查詢
借閱
二次添加新書(讀取已有的.xls并修改)
代碼:
import xlwtimport xlrddef read_old_data(row0_len): try:filename='.圖書.xls'old_data = []#讀取表格已有內(nèi)容data = xlrd.open_workbook(filename)sheet0 = data.sheet_by_index(0)nrows = sheet0.nrows #獲取該sheet中的有效行數(shù)print('Info:讀取到已有數(shù)據(jù)表格')print('有效行數(shù):',nrows)for i in range(nrows): for j in range(row0_len):old_data.append(sheet0.cell(i,j).value)print('共有舊的數(shù)據(jù):',len(old_data)) except IOError:print('Info: 沒(méi)有找到文件或讀取文件失敗/n1 =>新建圖書.xls文件')nrows=0 return old_data,nrowsdef new_book(): book = [] print_value=('書名','作者','編號(hào)','位置','數(shù)量') row0_len=len(print_value)#列數(shù) input_value=’’ ’’’ try: ’’’ (old_data,nrows)=read_old_data(row0_len)#打開(kāi)存儲(chǔ) book_excel = xlwt.Workbook() sheet1 = book_excel.add_sheet('books',cell_overwrite_ok=0) #寫入舊數(shù)據(jù): for i in range(nrows):for j in range(row0_len): sheet1.write(i,j,old_data[(i*row0_len)+j]) while(1):print('添加新書')#輸入for i in range(row0_len): print('請(qǐng)輸入:'+print_value[i]) input_value = input() #判斷是否輸出 if(input_value == ’q’):book_excel.save('圖書.xls')return book.append(input_value)#保存到硬盤for i in range(row0_len): sheet1.write(nrows,i,book[i])nrows=nrows+1book=[]#清空book緩存 returndef search(): #打開(kāi)excel book_excel = xlrd.open_workbook('圖書.xls') sheet1 = book_excel.sheets()[0] book_num =sheet1.nrows #while(1): #輸入書名 bookname = input('請(qǐng)輸入書名:') find_flag=0 #查找 for i in range(book_num):if(bookname == sheet1.cell_value(i,0)): if(int(sheet1.cell_value(i,4))>0):find_flag=1print('書名:',sheet1.cell_value(i,0))print('作者:',sheet1.cell_value(i,1))print('位置:',sheet1.cell_value(i,3))print('庫(kù)存(本):',sheet1.cell_value(i,4))return if(find_flag==1):print('查無(wú)此書。') returndef borrow(): #打開(kāi)excel book_excel =xlrd.open_workbook('圖書.xls') sheet1 = book_excel.sheets()[0] book_num = sheet1.nrows row0_len=5#5列 while(1):#輸入書名bookname = input('請(qǐng)輸入書名:')if(bookname == ’q’): return#查找for i in range(0,book_num): if(bookname == sheet1.cell(i,0).value):kucun=int(sheet1.cell_value(i,4))if(kucun>0): (old_data,nrows)=read_old_data(row0_len)#5列 book_excel_w = xlwt.Workbook('圖書.xls') sheet2 = book_excel_w.add_sheet('books',cell_overwrite_ok=True) #寫入舊數(shù)據(jù): for n in range(nrows):for j in range(row0_len): sheet2.write(n,j,old_data[(n*row0_len)+j])print('借到了!') sheet2.write(i,4,str(kucun-1)) print('剩余庫(kù)存:',kucun-1) book_excel_w.save('圖書.xls') break returndef main_window(): while(1):print('n====圖書管理系統(tǒng)====')print('1.新書加入')print('2.書籍查詢')print('3.圖書借閱')print('========4退出=======')x= (input('請(qǐng)輸入你的操作'))print(’n’)if(x == ’1’): new_book()elif(x==’2’): search()elif(x==’3’): borrow() elif(x==’4’): breakelse: print('輸入無(wú)效') returnmain_window()
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. vue-electron中修改表格內(nèi)容并修改樣式2. 利用FastReport傳遞圖片參數(shù)在報(bào)表上展示簽名信息的實(shí)現(xiàn)方法3. 以PHP代碼為實(shí)例詳解RabbitMQ消息隊(duì)列中間件的6種模式4. 微信小程序?qū)崿F(xiàn)商品分類頁(yè)過(guò)程結(jié)束5. 推薦一個(gè)好看Table表格的css樣式代碼詳解6. ASP常用日期格式化函數(shù) FormatDate()7. .NET 中配置從xml轉(zhuǎn)向json方法示例詳解8. ASP新手必備的基礎(chǔ)知識(shí)9. HTML中的XML數(shù)據(jù)島記錄編輯與添加10. 不使用XMLHttpRequest對(duì)象實(shí)現(xiàn)Ajax效果的方法小結(jié)
