简体   繁体   English

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

[英]Python only displays the last line of a .csv file

I'm trying to create a program (as part of a controlled assessment), which allows the user to enter 3 GTIN product codes from an excel (.csv) file. 我正在尝试创建一个程序(作为受控评估的一部分),该程序允许用户从excel (.csv)文件输入3个GTIN产品代码。 This then displays information about each product, and then asks how many of the product you want to buy, before giving a subtotal for that product. 然后显示有关每种产品的信息,然后在给出该产品的小计之前询问您要购买多少种产品。 Once all three codes are inputted, you can choose to display a receipt of all products. 输入所有三个代码后,您可以选择显示所有产品的收据。 The total works fine, but when I try to have it print the actual names of the products, it just prints the name of the product on the last line of the Excel file, three times on three separate lines. 总体效果很好,但是当我尝试使用它打印产品的实际名称时,它只会在Excel文件的最后一行上打印产品名称,在三行上分别打印三遍。

(I know this code is probably ridiculously clumsy, but I am basically as much of a novice as you'll find.) (我知道这段代码可能太笨拙了,但是我基本上是新手,就像您会发现的那样。)

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.")

but when I try to have it print the actual names of the products, it just prints the name of the product on the last line of the Excel file, three times on three separate lines. 但是,当我尝试用它打印产品的实际名称时,它只是在Excel文件的最后一行上打印了产品名称,在三行上分别打印了三遍。

This is normal and the expected behaviour of your code. 这是正常现象,也是代码的预期行为。

In fact, for each line of your CSV file, you set product1, item_name2 or item_name3. 实际上, 对于 CSV文件的每一行 ,您都设置了product1,item_name2或item_name3。

If the ordernum if the one you want to look at, you then ask the user for how much he want to buy/order. 如果ordernum是您要查看的那个,则您会询问用户要购买/订购的数量。

However and after that, the foreach loop will continue and read the file until the end (even if there is no more prints). 但是,此后,foreach循环将继续并读取文件直到结束(即使没有更多打印)。 Ending by setting the last CSV line value for your 3 item names. 最后,为3个商品名称设置最后一个CSV行值。

Simply return your function (or break from the for loop) at the end of if ordernum==product_ordered: 只需在if ordernum==product_ordered:的末尾返回函数 (或从for循环中中断if ordernum==product_ordered:

In order to stop reading file, as you don't need it ! 为了停止读取文件,您不需要它! It will then stop updating the item names and keep the one you want. 然后它将停止更新项目名称,并保留您想要的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM