Search code examples
pythondbf

dbf export cols to csv


Is it possible to export certain columns from a dbf database. The dbf table that I am trying to export has 150+ columns and I am only interested in 5. I am using the dbf module.

db = dbf.Table(dbf_link)
db.open()

dbf.export(db, filename='', fields='', format='csv', header=True)

gives me the error "unexpected keyword 'fields'"


Solution

  • When in doubt, check out the code (latest available here).

    There is an export method that looks like this:

    def export(
            table_or_records,
            filename=None,
            field_names=None,
            format='csv',
            header=True,
            dialect='dbf',
            encoding=None,
            ):
    

    So you will want something like:

    dbf.export(db, field_names=['field_1', 'field_2', ..., 'field_n'])
    

    where field_1, etc., are the actual field names you want.