Find an Item in a Sorted List

Dan Bishop danb_83 at yahoo.com
Tue Feb 26 22:50:56 EST 2002


hankc at nospam.com (HankC) wrote in message news:<3c7c1b5b.34558051 at news.la.sbcglobal.net>...
> Greetings:
> 
> I'm 2 days new to Python and hope I can get some pointers here...
> 
> I have a sorted, delimited text file such as:
> 
> Arty,1000
> Bobby,2000
> Charlie,3000
> 
> Knowing a name, I want to return the entire line from the file.  My
> approach/problems (any comments appreciated):

If your data file is small enough to fit in memory, you can use

dict = {}
datafile = open(..., "r")
for line in datafile.readlines():
   name = line.split(",")[0]
   dict[name] = line[:-1] # strip trailing \n
datafile.close()

Now, for example, dict["Bobby"] == "Bobby,2000"



More information about the Python-list mailing list