After the database has been opened, you can use the database object similarly to a dictionary. To change a value of an object in the database, assign a new value to the corresponding key using d[key] = value. To remove an object from the database, use del d[key] to reference the object by its specific key. Note The d.has_key(key) function can be extremely useful if you are not certain whether a specific key exists in the database. import anydbm flights = [] cancelled = ["1520", "1544"] deleted = ["1144"] def displayArrivals(header): print header print "========================================" for flight in flights: print ("Flight %s from %s arrives at %s" % (flight, cityDB[flight], timeDB[flight])) #Open DBM file for reading cityDB = anydbm.open("city.dbm", 'w') timeDB = anydbm.open("time.dbm", 'w') #Get keys flights = timeDB.keys() #Display arrivals displayArrivals("Arrivals") #Update DBM for flight in flights: for c in cancelled: if c == flight: timeDB[flight] = "CANCELLED" break for d in deleted: if d == flight: del timeDB[flight] del cityDB[flight] break #Display updataed arrivals flights = timeDB.keys() displayArrivals("Updated Arrivals") #Close DMB file cityDB.close() timeDB.close() update_dbm.py Arrivals ============================================= Flight 1144 from Dallas arrives at 230pm Flight 1045 from Los Angeles arrives at 320pm Flight 1520 from New York arrives at 420pm Updated Arrivals ============================================= Flight 1045 from Los Angeles arrives at 320pm Flight 1520 from New York arrives at CANCELLED Output from update_dbm.py code |