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

2024-05-16 02:59:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我希望用python开发一个应用程序,它需要搜索dBase III数据库文件(DBF)。我已经找了一段时间了,但是找不到任何关于如何做到这一点的好文档。我试过使用DBFpy,但找不到任何关于如何索引/搜索列的文档。我也试过遵循this thread中的建议,但显然我的DBF文件是“关闭的”,我查看了调用listed here,但无法确定如何“打开”该文件。是否有人推荐一个python模块来处理带有文档的DBF文件,或者指导我如何正确使用其他DBF python模块。谢谢!


Tags: 模块文件文档数据库应用程序herethisthread
1条回答
网友
1楼 · 发布于 2024-05-16 02:59:14

使用my dbf module基本流程如下:

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

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

lambda rec: rec.desired_field

或者你需要的那么复杂:

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

相关问题 更多 >