删除dbf文件/ lib中的空格

问题描述 投票:1回答:1

我需要使用pythondbflib过滤dbf文件。我在dbf库上使用了一些方法创建了一个类,因此我有一个用于过滤记录的方法search

def search(self, field_name: str, value: object) -> List:
    self._open_for_read()
    index = self._table.create_index(lambda rec: getattr(rec, field_name))
    records = index.search(match=(value,))
    return records

问题是dbf lib添加空格,或者我的dbf文件包含我看不到的空格。

因此,要查询包含desceng的名为Mattress的字段,我需要添加空格。

 records = hifis.search('desceng',  'Mattress                                                                        ')

空格的数量似乎取决于字段。

是否有删除空格的方法,所以我可以简单地用'Mattress'而不是'Mattress '进行查询?

谢谢!

python dbf
1个回答
0
投票

您可以做几件事:

  • 创建索引时修剪尾随空白:

    index = self._table.create_index(lambda rec: (getattr(rec, field_name) or '').strip())

  • 打开数据库时使用Char类,因为它会自动修剪尾随空白:

    self._table = dbf.Table(..., default_data_types={'C':dbf.Char}

© www.soinside.com 2019 - 2024. All rights reserved.