I have a Python program in which I am trying to use this rhyming dictionary to look up rhymes.

Part of the dictionary library setup works by a C program creating three gdbm .db files. The code that does this is publicly available here (from 'get the source' section), and the key part of the C gdbm builder compile.c looks like this:

#include <gdbm.h>

...<snip>...

int main(int argc, char *argv[]) {
  FILE *input;
  GDBM_FILE output;

...<snip>...

output = gdbm_open(argv[2], 0, GDBM_NEWDB, 0644, 0);

...<snip>...

gdbm_sync(output);

...<snip>...

words.db is created, and it can be successfully accessed from the bundled command line interface.

According to the library's example page, I should be able to use the python gdbm module to access the database.

However, I cannot. When I run the following:

import gdbm
words = gdbm.open('/usr/share/rhyme/words.db')

I get the following error:

    words = gdbm.open('/usr/share/rhyme/words.db')
gdbm.error: Bad magic number

Is there a file incompatibility problem? Do some gdbm files created in C not open with Python?

(This is with Python 2.7 on OSX 10.6.8. Python's gdbm was installed via MacPorts)

有帮助吗?

解决方案

How did you create the words.db file? It looks like the words.db is corrupted or in a format not supported by your specific version of the gdbm library on Mac OS X.

其他提示

Quoting the creator of Python (all kneel down and repeat we are not worthy! x3 :-)

Simple -- gdbm is sensitive to the byte order. Alas, I don't know what to do about it except converting it to a more portable format for transfer, or patching the gdbm source :-(

--Guido van Rossum (home page: http://www.python.org/~guido/)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top