尝试读取文件-python

尝试读取文件-python,python,Python,我试图在python上编写一个代码,它将读取一个文件,其中包含产品列表、每个产品的GTIN-8代码、每个产品的描述、库存和价格 到目前为止,我设法阅读了顶行,打印出了它的细节,但我不确定如何阅读下一行。有什么帮助吗?请尽量保持简单: 聚丙烯 我的人Doroskevic的代码: #csv is imported to read/write to the file import csv #Each Product is printed alongside it's GTIN-8 code and

我试图在python上编写一个代码,它将读取一个文件,其中包含产品列表、每个产品的GTIN-8代码、每个产品的描述、库存和价格

到目前为止,我设法阅读了顶行,打印出了它的细节,但我不确定如何阅读下一行。有什么帮助吗?请尽量保持简单:

聚丙烯

我的人Doroskevic的代码:

#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 purchase == "12346554":
    while(line):
        print ("Product: ", data[0])
        print ("GTIN-8 code: ", data[1])
        print ("Stock: ", data[2])
        print ("Description: ", data[3])
        print ("Price: ", data[4])
        line = file.readline()
        break
if purchase == "12346882":
    while(line):
        print ("Product: ", data[0])
        print ("GTIN-8 code: ", data[1])
        print ("Stock: ", data[2])
        print ("Description: ", data[3])
        print ("Price: ", data[4])
        line = file.readline()
        break

file.close()
例如:

输出:

readlines将把文件中的所有行读取到列表结构中。这将允许根据需要执行进一步的操作

参考:


您好,您能展示一下您的代码吗?您能提供一个链接到描述此策略的地方吗?如果没有提供链接,上述答案的质量是否不令人满意?如果是,您是否也可以提供标准的链接?如果你能取消否决票,我将不胜感激。我没有足够的声誉,我是新来的:D打了个勾,虽然抱歉,我觉得这页很难读understand@E.Doroskevic这几乎是所有的元@你能不能别再提这个复制品了,这个网站上还有其他问题没关系,如果你想的话就忽略它吧。你让我问另一个问题,我感谢你这么做。
path = 'c:/users/-e/my documents/text.txt'

with open(path, 'r') as file:
    data = file.readlines()

    for line in data:
        print(line, end='')
Are all the good times dead and gone?
Come and go and come and go (come and come and go)
I got a lot of friends who are stars
But some are just black holes

By Fall out boy - 27