读取文件的下一行

即时通讯新的这个网站,我知道很多人不是很高兴,当有人问一个问题以前问。 然而,我想问,尽pipe以前被问到,因为我发现的所有答案对我来说都没什么意义(即时通讯是Python的新手!),所以我想知道是否有人可以为我弄虚作假,或者直接纠正我的代码。

我在用户inputGTIN-8代码的地方编写了一个代码,然后在csv excel文件中search该代码,然后读取有关产品的适当信息(价格等)并将其打印出来。 但是我不能search文件的第二行出于某种原因。 这是我的代码:

#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 eg 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 eg 12346554")) ^^^^^This will cause a syntax error. You should remove this print as well