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

时间:2017-01-17 09:46:24

标签: python excel csv global-variables

我正在尝试创建一个程序(作为受控评估的一部分),允许用户从excel (。csv)文件中输入3个GTIN产品代码。然后显示有关每种产品的信息,然后在给出该产品的小计之前询问您要购买的产品数量。输入所有三个代码后,您可以选择显示所有产品的收据。总体工作正常,但是当我尝试打印产品的实际名称时,它只是在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.")

1 个答案:

答案 0 :(得分:2)

  

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

这是正常的以及代码的预期行为。

事实上,您的CSV文件的每行 ,您设置了product1,item_name2或item_name3。

如果您想查看的是ordernum,那么您可以询问用户他想要购买/订购的数量。

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

在结束时,只需返回您的功能(或中断来自for循环) if ordernum==product_ordered:

为了停止阅读文件,因为你不需要它! 然后它将停止更新项目名称并保留您想要的名称。