On Feb 9, 11:22 am, Oltmans <rolf.oltm...@gmail.com> wrote:
> Here is the scenario:
>
> It's a command line program. I ask user for a input string. Based on
> that input string I retrieve text from a text file. My text file looks
> like following
>
> Text-file:
> -------------
> AbcManager=C:\source\code\Modules\Code-AbcManager\
> AbcTest=C:\source\code\Modules\Code-AbcTest\
> DecConnector=C:\source\code\Modules\Code-DecConnector\
> GHIManager=C:\source\code\Modules\Code-GHIManager\
> JKLConnector=C:\source\code\Modules\Code-JKLConnector
>

Assuming the text-file is in the under-30Mb size, I would just read
the whole thing into a dict at startup, and then use the dict over and
over.

data = file(filename).read()
lookup = dict( line.split('=',1) for line in data.splitlines() if
line )

# now no further need to access text file, just use lookup variable

while True:
    user_entry = raw_input("Lookup key: ").strip()
    if not user_entry:
        break
    if user_entry in lookup:
        print lookup[user_entry]
    else:
        print "No entry for '%s'" % user_entry
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to