Python reading Berlkey DB-files made with Perl

Skip Montanaro skip at mojam.com
Wed Sep 22 10:35:14 EDT 1999


    Thomas> Can Python read Berkley DB files created with Perl ( simple
    Thomas> hashes, hashes of hashes etc. , stuff created with the
    Thomas> MLDBM-module etc. ) ??  If so, can anybody show just a simple
    Thomas> snippet doing this, just so that I can see how the code is
    Thomas> compared to perl code?

I've done this in the past.  First, before attempting anything, make sure
your Perl and Python installations are using the same version of Berkeley
DB.  If they aren't, you may be SOL.  Then, assuming your db file is named
spam.db, here's how to read it from Python:

    import bsddb
    db = bsddb.hashopen("spam.db")
    for k in db.keys():
	print db[k]
    db.close()
    print "And Bob's your uncle!"

That is, to a first approximation, it should behave like a dictionary.

You may have to change the hashopen call to either btopen or rnopen,
depending what the Perl program used to create the file.  I think hash files
are the most common however (perhaps the only choice).

Skip Montanaro | http://www.mojam.com/
skip at mojam.com | http://www.musi-cal.com/~skip/
847-971-7098   | Python: Programming the way Guido indented...




More information about the Python-list mailing list