How to modify dbf files in Python

Bill TP picture Bill TP · May 5, 2012 · Viewed 6.9k times · Source

Suppose I have different number of dbf files in some folders under the root directory, d:\myfolder. The content of a dbf file looks like the following:

Field1 
11110481123
12150480021
...

I want to add a field (say, Field1) that contains only the last 4 digits of the values in Field2.

Field1          Field2
11110481123     1123
12150480021     0021
...             ...

Then, I want to delete Field1.

How can I do the job for all the dbf files that are scattered across different folders in Python?

Answer

hyperboreean picture hyperboreean · May 5, 2012

You would need the module called dbf available via pypi (pip install dbf). Here's a snippet of how you can add and delete fields from a table:

import dbf

table = dbf.Table('t1', 'field1 N(12, 0)')
for record in ((11110481123,), (12150480021,)):
    table.append(record)
table.close()

# extend the existing table
dbf.add_fields('t1', 'field2 N(4, 0)')

table = dbf.Table('t1')
records = table.sql('select *')
for record in records:
    record.field2 = int(str(record.field1)[-4:])
table.close()

dbf.delete_fields('t1', 'field1')

Though it would be less computational intensive to just go over the first field and alter it to store the last 4 digits of its value.