gpt4 book ai didi

python - 读取文件的下一行

转载 作者:太空宇宙 更新时间:2023-11-03 16:53:43 26 4
gpt4 key购买 nike

我是这个网站的新手,我知道当有人问以前问过的问题时,很多人都不太高兴。然而,我想问,尽管之前有人问过,因为我发现的所有答案对我来说没有多大意义(我是Python新手!),所以我想知道是否有人可以为我简化它或直接纠正我的代码。

我正在编写一个代码,用户输入 GTIN-8 代码,它会在 csv excel 文件中搜索该代码,然后读取有关产品的适当信息(价格等...)并将其打印出来。但是由于某种原因我无法搜索文件的第二行。这是我的代码:

#csv is imported to read/write to the file
import csv

#Each Product is printed alongside it's GTIN-8 code and Price
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
print("~ Welcome to Toms bits and bobs ~")
print("Pencil, 12346554, £0.40")
print("50 Staples, 12346882, £1.00")
print("50 Paper Clips, 12346875, £1.20")
print("Large Eraser, 12346844, £1.50")
print("100 A4 Sheets, 12346868, £2.00")
print("100 A3 Sheets, 12346837, £2.50")
print("25 Byro Pens, 12346820, £2.20")
print("Handwriting Pen, 12346899, £5.50")
print("50 Split Pins, 12346813, £0.60")
print("Office Chair, 12346912, £25.00")
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")

#The file is opened and the user inputs the code for the product they
#wish to find.
file = open("Product_list.csv", "r")
purchase = input(print("Please enter the GTIN-8 code of the product you wish to purchase e.g 12346554"))
line = file.readline()
data = line.split(",")

if data[0] == purchase:
while(line):
print ("Product: ", data[1])
print ("GTIN-8 code: ", data[0])
print ("Stock: ", data[2])
print ("Description: ", data[3])
print ("Price: ", data[4])
line = file.readline()
break

else:
print("Product not found")


file.close()`

最佳答案

您正在阅读第二行,但由于 break,您永远没有机会使用它,因为如果您的代码进入那里,它总是会跳出 while 循环。只需删除它,您的代码就应该可以正常工作。

此外,假设该行的语法正确。

purchase = input(print("Please enter the GTIN-8 code of the product you wish to purchase e.g 12346554"))  
^^^^^This will cause a syntax error. You should remove this print as well

关于python - 读取文件的下一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35625356/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com