Find a word in shelve file : shelve « Database « Python






Find a word in shelve file


import sys, shelve, linecache
shIn = shelve.open('indexfiles')

for word in sys.argv[1:]:
    if not shIn.has_key(word):
         sys.stderr.write('Word %r not found in index file\n' % word)
         continue
    places = shIn[word]
    for fname, lineno in places:
        print "Word %r occurs in line %s of file %s:" % (word,lineno,fname)
        print linecache.getline(fname, lineno),


           
       








Related examples in the same category

1.Python object persistence for dictionary like objectPython object persistence for dictionary like object
2.Pickling and shelving dataPickling and shelving data