Search code examples
pythondbf

Defining Field names with special characters such as ^ in python dbf


I want to define a DBF table with field names which may contain ^ character. I am using the following table definition:

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

I am getting a fieldspecerror as following:

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

Is there a workaround (escape character) which allows me to define the headers mentioned above while creating dbf table?

I am using python 3.7


Solution

  • Original Answer

    To the best of my knowledge, the dbf field spec does not allow for non-alphanumeric characters in field names. The only possible work-around at the moment is to subclass Table and replace the failing method with one that allows the strange characters.


    Updated Answer

    Looking at the method in question (add_fields), replacing it would be a major endeavor. So as of dbf 0.98.0 one can use whatever strange characters one wants/needs in fields names, but dbf will generate a warning about it:

    <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)')
    

    To suppress that warning one can add:

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