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?
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).
0
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])
0
MK15Author Commented:
Hi Mark,

Type is <class '_dbm.dbm'>

Thanks
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

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.
0
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.
0

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
0
aikimarkCommented:
please post the file
0
Suhas .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
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.