在Python中打开和搜索dBase III(DBF)数据库

Sta*_*ski 5 python dbf

我正在寻找在python中开发一个需要搜索dBase III数据库文件(DBF)的应用程序.我一直在寻找一段时间,但我找不到任何关于如何做到这一点的好文档.我尝试过使用DBFpy,但找不到有关如何索引/搜索列的任何文档.我也尝试按照这个帖子中的建议,但显然我的DBF文件是"关闭的".我查看了此处列出的调用,但无法确定如何"打开"该文件.任何人都可以推荐一个python模块来处理带有文档的DBF文件,或者指导我如何正确使用其他DBF python模块.谢谢!

Eth*_*man 6

使用我的dbf模块基本流程如下:

import dbf

some_table = dbf.Table('/path/to/table.dbf')  # table is closed
some_table.open()
index = some_table.create_index(record_indexer)
.
.
.
records = index.search(match=(some_value,))   # returns a dbf.List of matching records
Run Code Online (Sandbox Code Playgroud)

并且record_indexer是一个返回适当索引值的函数; 它可以很简单

lambda rec: rec.desired_field
Run Code Online (Sandbox Code Playgroud)

或者根据需要复杂:

def record_indexer(record):
    if record.that_field == 'bad value':
        return dbf.DoNotIndex             # record is ignored
    return record.this_field, record.other
Run Code Online (Sandbox Code Playgroud)