Python只显示.csv文件的最后一行

我试图创build一个程序(作为受控评估的一部分),允许用户从excel (.csv)文件中input3个GTIN产品代码。 然后显示关于每个产品的信息,然后在给出该产品的小计之前询问您想要购买多less产品。 一旦input了所有三个代码,就可以select显示所有产品的收据。 总的工作正常,但是当我尝试打印产品的实际名称时,它只是在Excel文件的最后一行打印产品的名称,在三个单独的行上打印三次。

(我知道这个代码可能非常笨拙,但我基本上是一个新手,你会发现。)

import sys while 1==1: def gtin1(): global total global product1 product_ordered=input("Input GTIN ") f=open("Task 2 Product Spreadsheet.csv","r") for line in f: items =line.split(",") ordernum=items[0] product1=items[1] price=float(items[2]) in_stock=int(items[3]) if ordernum==product_ordered: print("Items Ordered: ") print("Item name: ", product1) print("Number in stock: " , in_stock) print("Price: £",price) number = int(input("Input the number of items to be ordered: ")) total = int(price) * int(number) if number <= in_stock: print("The total price is £" + str(total)) print ("") else: print ("There is insufficient stock.") print ("") def gtin2(): global total2 global item_name2 product_ordered=input("Input GTIN ") f=open("Task 2 Product Spreadsheet.csv","r") for line in f: items =line.split(",") ordernum=items[0] item_name2=items[1] price=float(items[2]) in_stock=int(items[3]) if ordernum==product_ordered: print("Items Ordered: ") print("Item name: ", item_name2) print("Number in stock: " , in_stock) print("Price: £",price) number = int(input("Input the number of items to be ordered: ")) total2 = int(price) * int(number) if number <= in_stock: print("The total price is £" + str(total2)) print ("") else: print ("There is insufficient stock.") print ("") def gtin3 (): global total3 global item_name3 product_ordered=input("Input GTIN ") f=open("Task 2 Product Spreadsheet.csv","r") for line in f: items =line.split(",") ordernum=items[0] item_name3=items[1] price=float(items[2]) in_stock=int(items[3]) if ordernum==product_ordered: print("Items Ordered: ") print("Item name: ", item_name3) print("Number in stock: " , in_stock) print("Price: £",price) number = int(input("Input the number of items to be ordered: ")) total3 = int(price) * int(number) if number <= in_stock: print("The total price is £" + str(total3)) print ("") else: print ("There is insufficient stock.") print ("") break def receipt(): receipt = int(total) + int(total2) + int(total3) print ("") print ("") print (product1) print (item_name2) print (item_name3) print ("The total price of your order is: £" + str(receipt)) print ("") print ("") menu = input("You may enter 3 GTIN codes. Press '1' for your first code, '2' for your \ second, and '3' for your third, 'r' to see your receipt, or 'c' to exit the program.") if menu == "1": gtin1() elif menu == "2": gtin2() elif menu == "3": gtin3() elif menu == "r": receipt() elif menu == "c": print ("Thank you for using the program.") sys.exit() else: print("Enter a valid command.") 

但是当我尝试打印产品的实际名称时,它只是在Excel文件的最后一行打印产品的名称,在三个单独的行上打印三次。

这是正常的,你的代码的预期行为。

实际上, 对于 CSV文件的每一行 ,您都设置了product1,item_name2或item_name3。

如果ordernum如果你想看,那么你问用户多less他想买/订单。

但是,之后,foreach循环将继续并读取文件直到结束(即使没有更多的打印)。 通过为您的3个项目名称设置最后一个CSV行值结束。

if ordernum==product_ordered:if ordernum==product_ordered:只需返回你的函数 (或者从for循环中断

为了停止阅读文件,因为你不需要它! 它会停止更新项目名称,并保持你想要的。