Dear Feenix45,
The best way is to enclose the code in bracket to maintain the
Indentation which is vita important. Just wrap your code tags
Is this what you originally wrote??
Is your application GUI or Console?
def ID_search(prod_id):
infile=open("tab.txt","r")
line=infile.readline()
while line!="":
x=string.split((line),",")
if prod_id==float(x[0]):
print "Product is", x[1]
line=infile.readline()
infile.close()
evstevemd
Senior Poster
3,713 posts since Jun 2007
Reputation Points: 462
Solved Threads: 392
Also, you open the file and read the entire file for each ID entered. This is time consuming (in computer time) and so you should consider reading the file once and placing the records in a dictionary, indexed on ID, and using the dictionary to look up the info as it is entered.
woooee
Nearly a Posting Maven
2,454 posts since Dec 2006
Reputation Points: 777
Solved Threads: 714
Here's an example of reading the file into a dictionary:
def load_IDs():
infile=open("tab.txt","r")
lines=infile.readlines()
infile.close()
my_IDs = {}
for line in lines:
data=line.strip().split(",")
my_IDs[ data[ 0 ] ] = data[ 1 ]
return my_IDs
jlm699
Veteran Poster
1,112 posts since Jul 2008
Reputation Points: 355
Solved Threads: 292
You're reading the contents of the file into a dictionary, not a list.
What do you mean work directly from the file?
jlm699
Veteran Poster
1,112 posts since Jul 2008
Reputation Points: 355
Solved Threads: 292