我是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文件的内容显示到突然将它们全部转换为对象(然后可以单独删除、添加新书等)的进展。我花了几天时间在谷歌和YouTubing上搜索,但什么也没找到,所以我来这里寻求帮助。非常感谢。
发布于 2022-06-20 17:59:48
您似乎希望使用在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)您将看到您的对象创建。
而且,如果你真的不需要每个列表中的空'‘,就像我建议的那样,你可以用列表解压来完成它,它会更多地是丙酮的。如下所示:
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
...如果您使用dataclass并打印您的对象,您将得到一些可以帮助您的内容:
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
...发布于 2022-06-20 17:35:50
您就快到了!inventory是一个列表(因此是方括号)。
要创建Book对象,只需更新提供给您的示例:
books.append(Book(inventory[0],inventory[1],inventory[2],inventory[3],inventory[4],inventory[5]))若要打印而不带方括号,请将打印语句更新为:
print(' '.join(inventory))发布于 2022-06-20 20:36:06
上面Emmacb's answer回答了你的问题。
添加一下,您可以在创建@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://stackoverflow.com/questions/72690872
复制相似问题