python - 删除 dbf 文件/lib 中的空格
问题描述
我需要使用 python dbf
lib 过滤一个 dbf 文件。我在 dbf lib 上创建了一个包含一些方法的类,所以我有一个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 文件包含我看不到的空格。
因此,要查询一个名为 contains 的字段desceng
,Mattress
我需要添加空格。
records = hifis.search('desceng', 'Mattress ')
空格的数量似乎取决于字段。
有没有办法删除空格,所以我可以简单地用'Mattress'
而不是查询'Mattress '
?
谢谢!
解决方案
您可以做几件事:
创建索引时修剪尾随空格:
index = self._table.create_index(lambda rec: (getattr(rec, field_name) or '').strip())
打开数据库时使用
Char
该类,因为它会自动修剪尾随空格:self._table = dbf.Table(..., default_data_types={'C':dbf.Char}
推荐阅读
- mysql - 通过 SSH 隧道进行 MySql 复制
- javascript - 如何从对象数组中提取属性并对其进行切片?
- javascript - 仅当父对象存在时才验证嵌套对象
- xml - Android Studio 在 xml 布局文件中找不到要访问的声明
- c# - 从磁盘加载 WindowsFormsControlLibrary DLL,然后添加到控件,C#
- node.js - 登录到 [sap] cloud Foundry 并使用 nodejs 包装器获取 api 响应
- javascript - 如果比较值出现在另一层中,则无法删除数组
- vue.js - VueJS 显示动态模态组件
- javascript - MS CRM 2013 - 从链接实体中检索属性以创建 html webresource
- javascript - javascript:是否可以为“未定义”赋值?(You-Dont-know JS 的一个问题)