簡體   English   中英

Python 中的列表索引超出范圍錯誤(讀取 CSV 文件)

[英]list index out of range error in Python ( reading CSV file)

I am trying to create a shop in Python using a procedural approach rather than Object Oriented and then I created this function which reads my CSV file with the shop stock, except for the first row of the CSV file that contains the "cash available" in商店。

我試圖然后訪問價格和數量,但我得到了錯誤:

p=(row[0], float(row[1]))
IndexError: list index out of range

這是我的參考代碼:

def createAndStockShop():
    s=Shop()
    with open("Stock.csv")as csv_file:#reading our stock file 
        csv_reader=csv.reader(csv_file,delimiter=',')#creating csv reader variable setting up delimiter as ","
        first_row=next(csv_reader)
        s.cash=(first_row[0])#extracting shop cash from first row of the file
        for row in csv_reader:#using for loop to go through the file 
            p=(row[0], float(row[1]))
            ps=ProductStock(p,float(row[2]))
            s.stock.append(ps)
            print(ps)
    return s#returning shop

作為參考,這是文件“Stock.csv”的外觀: csv 文件我正在使用此代碼打開並包含我的股票

此外,這些是我為 Product stock 和 shop 創建的類,以提供更多上下文:

@dataclass #Creating first data class for Products in shop
class Product:
    name: str #values are "name" as string and price as float to include decimals
    price: float = 0.0

@dataclass #Creating data class for the stock in shop
class ProductStock:
    product:Product
    quantity:int

@dataclass #Dataclass for shop, values cash as a float and stock as list
class Shop():
    cash: float = 0.0
    stock: List[ProductStock] = field(default_factory=list)

非常感謝你們,最后我按照您的建議刪除了文件中的空行並且它起作用了。:非常感謝,但是現在告訴我一個不同的錯誤。 ''' " 第 126 行。如果 item.product.name == prod.product.name 和 item:quantity <= prod:quantity:#checking 列表中的項目名稱是否與庫存中的名稱匹配 AttributeError: 'tuple' object沒有屬性“名稱””

''' 我理解它指的是這段代碼:

#defining method to check the shop stock: 
def checking_stock(c, s): #parameters for this function will be "c" ( customer) and s (shop)
    for item in c.shopping_list:#looping through the items in customer shopping list 
        for prod in s.stock:
                if item.product.name == prod.product.name and item.quantity <= prod.quantity:#checking if item name in list matches the name in stock
                    #also if quantity needed is less than the amount or product in stock
                    print(item, item.quantity, prod.quantity)#if this is the case, print item quantity and agree with the purchase
                    print("\nPerfect! you can proceed.")
                
                elif item.product.name == prod.product.name and item.quantity > prod.quantity:#else if the product amount requested by client is bigger than stock
                    print(f"Hey! So sorry! We do not have enough stock of: {item.product.name}, please select a different amount.")#printing error message
                    main()

如前所示,我為 Product 創建的 class 有一個屬性名稱:

@dataclass #Creating first data class for Products in shop
class Product:
    name: str #values are "name" as string and price as float to include decimals
    price: float = 0.0

跟這個有關系嗎? 太感謝了。

根據您發布的圖像...第 9,10 行是空行。 刪除它們並重試。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM