MK15
asked on
Python - NDBM open and read
Hi,
How do i go about opening an NDBM file in Python and printing its contents?
I am not sure how to traverse through the database?
Any help would be very much appreciated.
M
How do i go about opening an NDBM file in Python and printing its contents?
import dbm
db = dbm.ndbm.open('/Users/20151027.DBM','r')
I am not sure how to traverse through the database?
Any help would be very much appreciated.
M
At the point where your second statement has executed, your db variable should resemble a dictionary object (key:value pairs).
You should check the type on your db object
print type(db)
The result should be <type 'dict'>
If it is you can loop through it and print the key/value pairs out like this:
for key in db:
print key + ':' + str(db[key])
print type(db)
The result should be <type 'dict'>
If it is you can loop through it and print the key/value pairs out like this:
for key in db:
print key + ':' + str(db[key])
ASKER
Hi Mark,
Type is <class '_dbm.dbm'>
Thanks
Type is <class '_dbm.dbm'>
Thanks
Ok I'm pretty sure you should be able to loop through the properties the same way as I explained.
If you print db you should see all the key/value pairs. If that is the case try the loop code I gave you to print them all out.
If you print db you should see all the key/value pairs. If that is the case try the loop code I gave you to print them all out.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi,
Thanks for all your help. I just realised that the file its reading might not be of-type ndbm. I tried running whichdb and it returned blank. I have tried GDBM, however this returns a 'Magic Number Error'. How can i find out what type of DB it is?
Sorry to waste all your time!
M
Thanks for all your help. I just realised that the file its reading might not be of-type ndbm. I tried running whichdb and it returned blank. I have tried GDBM, however this returns a 'Magic Number Error'. How can i find out what type of DB it is?
Sorry to waste all your time!
M
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
No comment has been added to this question in more than 21 days, so it is now classified as abandoned.
I have recommended this question be closed as follows:
Split:
-- Walter Ritzel (https:#a41188309)
-- aikimark (https:#a41192829)
If you feel this question should be closed differently, post an objection and the moderators will review all objections and close it as they feel fit. If no one objects, this question will be closed automatically the way described above.
suhasbharadwaj
Experts-Exchange Cleanup Volunteer
I have recommended this question be closed as follows:
Split:
-- Walter Ritzel (https:#a41188309)
-- aikimark (https:#a41192829)
If you feel this question should be closed differently, post an objection and the moderators will review all objections and close it as they feel fit. If no one objects, this question will be closed automatically the way described above.
suhasbharadwaj
Experts-Exchange Cleanup Volunteer