如何使用Python读取Berkeley DB文件?

时间:2014-04-08 20:04:41

标签: python berkeley-db

如何使用Python阅读Berkeley DB文件?

我有这个档案......

    [root@dhcp-idev1 ndb]# file dhcp.ndb 
    dhcp.ndb: Berkeley DB (Btree, version 9, native byte-order)

...所以我想我能做到这一点......

    [root@dhcp-idev1 ndb]# python2.3 
    Python 2.3.4 (#1, Jul 16 2009, 07:01:37) 
    [GCC 3.4.6 20060404 (Red Hat 3.4.6-11)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import anydbm
    >>> anydbm.open( './dhcp.ndb' )

...但我收到此错误消息......

    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
      File "/usr/lib/python2.3/anydbm.py", line 80, in open
        raise error, "db type could not be determined"
    anydbm.error: db type could not be determined
    >>> 

......我做错了什么?

1 个答案:

答案 0 :(得分:1)

以下是与此错误相关的代码来自anydbm.py

from whichdb import whichdb
  result=whichdb(file)
  if result is None:
     # db doesn't exist
     if 'c' in flag or 'n' in flag:
        # file doesn't exist and the new
        # flag was used so use default type
        mod = _defaultmod
     else:
        raise error, "need 'c' or 'n' flag to open new db"
  elif result == "":
     # db type cannot be determined
     raise error, "db type could not be determined"

如果whichdb可以打开文件但无法确定要使用的库,则会返回空字符串。

所以问题是为什么它无法确定库。可能是未安装打开此DB文件所需的库。

 anydbm is a generic interface to variants of the DBM database — dbhash (requires 
 bsddb), gdbm, or dbm. If none of these modules is installed, the slow-but-simple
 implementation in module dumbdbm will be used.

所以要么你缺少dumbdbm模块(导入它并使用它而不是anydbm),要么你需要安装其他库dbhash gdbm, dbm来打开文件。