如何使用Python读取Berkeley DB文件?
如何使用Python读取Berkeley DB文件 我有这个档案如何使用Python读取Berkeley DB文件?,python,berkeley-db,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
[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行,是否在中?
文件“/usr/lib/python2.3/anydbm.py”,第80行,打开
引发错误,“无法确定数据库类型”
anydbm.error:无法确定数据库类型
>>>
。。。我做错了什么?以下是与此错误相关的代码,来自
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
可以打开文件,但无法确定要使用的库,则返回空字符串
所以问题是为什么它不能确定图书馆。可能是打开此数据库文件所需的库未安装
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
来打开该文件 您是否尝试过直接使用dbm.open
、gdbm.open
或dbhash.open
?另外,dbm
后端为给定的文件名添加了一个.db
扩展名,因此请尝试将文件重命名为dhcp.db
,并使用dbm.open('dhcp')
打开它。谢谢您的回答!我已经安装了dumbdb
。dumbdbm
应该在Python2.X中。只需尝试使用dumbdbm.open('./dhcp.ndb')
。对于Python3.X,使用dbm.dumb.open(文件名)
。别忘了导入dumbdbm。感谢Arvind的工作原理>>导入dumbdbm>>>dumbdbm.open('./dhcp.ndb'){}