-2

I am trying to write a code (on python) that will read a file with a list of products, a GTIN-8 code for each, a description, stock and price for each individual product.

So far I managed to read the top line, printing out its details, but I am unsure how to read the next line down. Any Help? Please try and keep it simple :)

PP

Code for my man 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()

1 Answers1

-3

Example:

path = 'c:/users/-e/my documents/text.txt'

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

    for line in data:
        print(line, end='')

Output:

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

Function .readlines() will read all lines within file into a list structure. This will allow further operations to be carried out as needed.

Reference:

Python Documentation
Python TutorialsPoint

e.doroskevic
  • 1,969
  • 14
  • 25
  • Could you please provide a link to where this polcy is described? If no link is provided, is the quality of the above answer not satisfactory? If yes, could you also provide a link to the criteria? Else - I would appreciate if you would remove the down-vote. – e.doroskevic Feb 09 '16 at 11:50
  • I do not have enough reputation (im new :D) gave it a tick though – Python Peter Feb 09 '16 at 11:55
  • apologise I find the page hard to understand – Python Peter Feb 09 '16 at 11:56
  • @E.Doroskevic It is pretty much all over meta. http://meta.stackexchange.com/questions/10841/how-should-duplicate-questions-be-handled – JDurstberger Feb 09 '16 at 12:04
  • @Altoyr can you please stop mentioning the duplicate, it doesn't matter that there is another question on this website, just ignore it if you want to. You referred me to another question and I thank you for doing so. – Python Peter Feb 09 '16 at 12:09
  • @E.Doroskevic See http://stackoverflow.com/help/duplicates . See the part about *"search and research"* in http://stackoverflow.com/help/how-to-ask . See the part about *"Please look around to see if your question has been asked before"* in http://stackoverflow.com/help/on-topic – AdrianHHH Feb 09 '16 at 12:09
  • @AdrianHHH that's cool, where does it state that people should be down-voted for providing answers to duplicate questions? I think this should be taken into a chat for a broader discussion – e.doroskevic Feb 09 '16 at 12:13
  • @E.Doroskevic Already discussed at great length. See the answers to http://meta.stackoverflow.com/questions/281793/stance-on-answering-bad-questions and its duplicates. – AdrianHHH Feb 09 '16 at 13:18