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

标签 python dbf

我想用 Python 开发一个应用程序,它需要搜索 dBase III 数据库文件 (DBF)。我已经搜索了一段时间,但找不到任何关于如何执行此操作的好文档。我试过使用 DBFpy 但找不到任何关于如何索引/搜索列的文档。我也尝试遵循 this thread 中的建议但显然我的 DBF 文件已“关闭”。我看了电话listed here但无法确定如何“打开”文件。谁能推荐一个 python 模块来处理带有文档的 DBF 文件,或者指导我如何正确使用其他 DBF python 模块。谢谢!

最佳答案

使用 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

关于python - 在 Python 中打开和搜索 dBase III (DBF) 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14008534/

相关文章:

python - 任意数量嵌套 for 循环的算法

visual-foxpro - 在DBF中, "Ñ"字符显示为 "¥"

c# - 使用 C# 获取 .dbf 文件中的数据

.net - 从 C# 处理 .dbf 的最佳方式

Python 对象包装器

Python Pandas - 将 groupby 函数的结果返回到父表

python - 网站 map 爬虫中永无休止的 for 循环

c# - C# 中的错误 DBF 文件读取器

c# - 从 C# 处理 dbf 的最快方法

python - 如何解析txt文件末尾的json格式文本