我是 Python 的初學者,在學校專案中遇到了麻煩。我正在嘗試基于名為 books.txt 的檔案創建圖書館庫存管理系統
首先,這里是books.txt的原始資料
Mastering Windows Server 2019 - Second Edition;Krause,Jordan;978-1789804539;005.4476/KRA;5;3;
Windows Server 2019 & PowerShell All In One For Dummies;Perrot,Sara;978-1119560715;005.4476/PER;10;2;
Windows Server Automation with PowerShell Cookbook - Fouth Edition;Lee,Thomas;978-1800568457;005.4476/LEE;3;1;
Python Cookbook: Recipes for Mastering Python 3;Beazley,David;978-1449340377;005.133/BEA;10;8;
Automate the Boring Stuff With Python;Sweigart,Al;978-1593275990;005.133/SWE;10;10;
Head First Python - 2nd Edition;Barry,Paul;978-1491919538;005.133/BAR;4;2;
Python Crash Course - 2nd Edition;Matthes,Eric;978-1593279288;005.133/MAT;12;8;
Python for Dummies;Maruch,Stef;978-0471778646;005.133/MAR;5;0;
Beginning Programming with Python for Dummies;Mueller,John Paul;978-1119457893;005.133/MUE;7;5;
Beginning COBOL for Programmers;Coughlan,Michael;978-1430262534;005.133/COU;1;0;
所以我在這里要做的是將這些書籍物件的串列存盤在一個變數中。該程式的目的是修改書籍物件串列,而不是直接修改 .txt 檔案。完成后,用戶可以保存更改,然后覆寫 .txt 檔案。無論如何,我已經嘗試了很多不同的東西,現在我覺得這個功能在讀取和分割檔案中的行以創建串列方面讓我最接近。
#Apparently 'with open' stops you from having to close the file.
#Copy of display() for the purposes of playing around.
def inventory3():
print("\nName \t \t Author \t \t ISBN \t \t Call Number \t \t Stock \t \t Loaned")
with open("books.txt", "r") as inventoryfile:
for line in inventoryfile:
strip_lines=line.strip()
inventory = strip_lines.split(";")
print(inventory)
這會正確顯示 books.txt 檔案中的所有行(我不希望顯示方括號,但這是以后的問題)并且我從測驗中知道(像 test = inventory[-3:] 這樣的東西)它作為串列正確運行。現在的目標是“索引存盤的串列”來創建書籍物件,顯然我創建的每個書籍物件都應該存盤在一個單獨的串列中。這是我提供的示例。
books.append(Book(line[0],line[1],line[2],line[3],line[4],line[5]))
我之前創建了一個書籍類,就像這樣
class Book:
def __init__(self, title, author, isbn, callnumber, stock, loaned):
self.title = title
self.author = author
self.isbn = isbn
self.callnumber = callnumber
self.stock = stock
self.loaned = loaned
def getTitle(self):
return self.title
def getAuthor(self):
return self.author
def getISBN(self):
return self.isbn
def getCallNumber(self):
return self.callnumber
def getStock(self):
return self.stock
def getLoaned(self):
return self.loaned
我對如何將這兩者聯系在一起有點困惑。我沒有看到從顯示 txt 檔案的內容到突然將它們全部轉換為物件(然后可以單獨洗掉、添加新書等)的進展。我花了幾天的時間在 Google 和 YouTubing 上搜索,但一無所獲,所以我在這里尋求幫助。非常感謝。
uj5u.com熱心網友回復:
你快到了!inventory是一個串列(因此是方括號)。
要創建 Book 物件,只需更新您獲得的示例:
books.append(Book(inventory[0],inventory[1],inventory[2],inventory[3],inventory[4],inventory[5]))
要列印不帶方括號,請將您的列印陳述句更新為:
print(' '.join(inventory))
uj5u.com熱心網友回復:
您似乎想使用在inventory3 中創建的串列來實體化您的Book 類。在這種情況下,您可以嘗試稍微更改您的函式并添加一個回傳,如下所示:
def inventory3():
inventory = []
print("\nName \t \t Author \t \t ISBN \t \t Call Number \t \t Stock \t \t Loaned")
with open("books.txt", "r") as inventoryfile:
for line in inventoryfile:
strip_lines=line.strip()
inventory_line = strip_lines.split(";")[:-1] # Use-1 to get rid of all empty double quotes ('') in the end of each list
print(inventory_line)
inventory.append(inventory_line)
return inventory
有了這個,你可以這樣做:
inventory = inventory3()
books = []
for book_details in inventory:
books.append(Book(book_details[0],book_details[1], book_details[2],book_details[3],book_details[4],book_details[5]))
print(books)
您將看到創建的物件。
而且,如果你真的不需要每個串列的 and 中的空 '',正如我所建議的,你可以通過串列解包來做到這一點,它會更加 Pythonic。像這樣:
inventory = inventory3()
books = []
for book_details in inventory:
books.append(Book(*book_details))
print(books)
*book_details將與 完全相同book_details[0],book_details[1], book_details[2],book_details[3],book_details[4],book_details[5]。
編輯
正如評論中所說,我正在添加一個使用資料類的示例。
from dataclasses import dataclass
@dataclass
class Book:
title:str
author:str
isbn:str
callnumber:str
stock:str
loaned:str
# Your Functions
...
如果你使用資料類并列印你的物件,你會得到一些可以幫助你的東西:
Book(title='Mastering Windows Server 2019 - Second Edition', author='Krause,Jordan', isbn='978-1789804539', callnumber='005.4476/KRA', stock='5', loaned='3')
代替:
<__main__.Book object at 0x000001F1D23BFFA0>
如果你想要自己定義的東西,你可以實作你 book 類的 repr 方法:
class Book:
def __init__(self, title, author, isbn, callnumber, stock, loaned):
self.title = title
self.author = author
self.isbn = isbn
self.callnumber = callnumber
self.stock = stock
self.loaned = loaned
def __repr__(self):
return self.title '/' self.author
# Your Functions
...
uj5u.com熱心網友回復:
Emmacb上面的回答回答了你的問題。
只是要補充一點,您可以@dataclass在創建Book類時使用裝飾器。這將減少代碼長度,并提高可讀性。也不需要定義Book.getXXX方法,因為這些方法可以通過屬性名稱直接訪問
from dataclasses import dataclass
@dataclass
class Book:
title: str
author: str
isbn: str
callnumber: str
stock : str
loaned : str
book_one = Book('Title', 'Author','ISBN', 'Call Number', 'Stock', 'loaned')
book_one_title = book_one.title
print(book_one_title)
這需要您指定資料的型別,在此示例中,我假設所有字串在哪里。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/493739.html
