Convert .csv file into .dbf using Python?

djq picture djq · Dec 14, 2010 · Viewed 10.6k times · Source

How can I convert a .csv file into .dbf file using a python script? I found this piece of code online but I'm not certain how reliable it is. Are there any modules out there that have this functionality?

Answer

Ethan Furman picture Ethan Furman · Jul 13, 2011

Using the dbf package you can get a basic csv file with code similar to this:

import dbf
some_table = dbf.from_csv(csvfile='/path/to/file.csv', to_disk=True)

This will create table with the same name and either Character or Memo fields and field names of f0, f1, f2, etc.

For a different filename use the filenameparameter, and if you know your field names you can also use the field_names parameter.

some_table = dbf.from_csv(csvfile='data.csv', filename='mytable',
        field_names='name age birth'.split())

Rather basic documentation is available here.

Disclosure: I am the author of this package.