Python-How to stop code checking every line of a text file individually? -


so i'm working on code has text file stores list of 'products', , user should able input product gtin-8 code want , receipt it. i've made when user inputs gtin-8 code, code looks through products text file find matching gtin-8 code. when does, should line of code , write text file called 'receipt' (i couldn't think of other way this). should 'product found' or 'that product doesn't exist' depending if there match. however, code checks every individual line of code , says on screen every line of code if there's match or not. need have individual line of code can take information of line, don't want code check every single line. overview of entire file , pick out specific line. hope makes sense.

with open("productsfile.txt") f:     found = false     line in f:         if productswanted in line:             receipt=open("receiptfile.txt","a")             receipt.write("%r x%r\n" % (line, amountofproducts))             receipt.close()             print("product found!")             found = true     if not found:         print("that product not exist") 

(i deleted screenshot/link , put in text, sorry if putting in screenshot/link wrong thing do) thank you!!!

how many products there?

if there fewer than, say, million, consider reading contents of file dictionary, keyed on gtin-8 code.

(does make sense?)


Comments

Popular posts from this blog

inversion of control - Autofac named registration constructor injection -

verilog - Systemverilog dynamic casting issues -

ios - Change Storyboard View using Seague -