I am researching database files and also the dbm module in Python 3.1.3, and am getting trouble using a few of the techniques in the anydbm module in Python 2.
The secrets method works fine,
import dbm db = dbm.open('dbm', 'c') db['modest'] = 'mouse' db['dream'] = 'theater' for key in db.keys(): print(key)
but products and values,
for k,v in db.items(): print(k, v) for val in db.values(): print(val)
mention an AttributeError: '_dbm.dbm' object doesn't have attribute 'items'.
for key in db: print(key)
will get a TypeError: '_dbm.dbm' object isn't iterable.
Do these techniques simply not operate in the dbm module in Python 3? If that is true, can there be other things which i can use rather?
I believe this is dependent which implementation it selects to make use of. On my small system, dbm in Python 3 selects to make use of ndbm, which is the same as the
dbm module in Python 2. After I use that module clearly, I begin to see the same restrictions.
It seems anydbm in Python 2 selects dumbdbm, that is reduced, but does offer the full dictionary interface.
You might like to consider the
shelve module, both in Python 2 and three, which adds another layer of these connects (permitting you to definitely store any pickleable object).
The objective of these kind of simple databases would be to behave as key/value stores. It you would like all of the values, you need to iterate total the secrets. Ie:
values = [db[key] for key in db.keys()]
That won't be fast. It might me this kind of key/value store is not really the thing you need. Possibly SQLite could be better?
Nevertheless, you have access to dumbdbm underneath the title of dbm.dumb in Python 3.
>>> import dbm >>> db = dbm.dumb.open('dbm', 'c') >>> >>> db['modest'] = 'mouse' >>> db['dream'] = 'theater' >>> for key in db.keys(): ... print(key) ... b'modest' b'dream' >>> for k,v in db.items(): ... print(k, v) ... b'modest' b'mouse' b'dream' b'theater' >>> for val in db.values(): ... print(val) ... b'mouse' b'theater'