Python - NDBM open and read

Hi,

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')

Open in new window


I am not sure how to traverse through the database?

Any help would be very much appreciated.

M
MK15Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

aikimarkCommented:
At the point where your second statement has executed, your db variable should resemble a dictionary object (key:value pairs).
Mark BradyPrincipal Data EngineerCommented:
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])
MK15Author Commented:
Hi Mark,

Type is <class '_dbm.dbm'>

Thanks
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Mark BradyPrincipal Data EngineerCommented:
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.
Walter RitzelSenior Software EngineerCommented:
Since you are using the DBM interface, in python 2.X or ndbm for python 3, it is useful to know that the dictionary interface is not fully implemented on them, so the correct way to do that is on those cases is:
to print keys:
for key in db.keys():
    print(key)

to print values
for key in db.keys():
    print(db[key])

Open in new window


If you try thinks like:
for k,v in db.items():
    print(k, v)

for val in db.values():
    print(val)

for key in db:
    print(key)

Open in new window

you'll probably see errors.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MK15Author Commented:
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
aikimarkCommented:
please post the file
Suhas .Senior QA ManagerCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Python

From novice to tech pro — start learning today.