首页 > 解决方案 > 在 python dbf 中使用特殊字符定义字段名称,例如 ^

问题描述

我想定义一个字段名称可能包含 ^ 字符的 DBF 表。我正在使用下表定义:

tbl_SubDBF = dbf.Table("..//output//Ttest.dbf", \
                       'B^AWG C(50);    C^PTYPE C(200)')

我收到如下字段规范错误:

field names must start with a letter, and can only contain letters, digits, and _

是否有一种解决方法(转义字符)允许我在创建 dbf 表时定义上述标题?

我正在使用 python 3.7

标签: pythondbf

解决方案


原始答案

据我所知,dbf 字段规范不允许在字段名称中使用非字母数字字符。目前唯一可能的解决方法是子类Table化并用允许奇怪字符的方法替换失败的方法。


更新的答案

查看有问题的方法 ( add_fields),替换它将是一项重大的努力。因此,dbf 0.98.0一个人可以在字段名称中使用任何想要/需要的奇怪字符,但 dbf 会生成一个警告:

<module>:<line_no>: FieldNameWarning: "p^type invalid:  field names should start with a letter, and only contain letters, digits, and _
some_dbf.add_fields('p^type C(25)')

要抑制该警告,可以添加:

import warnings
warnings.filterwarnings('ignore', '', dbf.FieldNameWarning)

推荐阅读