how can I create a new .dbf file with python (solution with ethanfurman/dbf package will be preferred)

Question:

I couldn’t find any public method in ethanfurman/dbf for writing data to a given file path instead of modifying an existed file. I wanna create a dbf file from a list of tuple and the "field_specs" string.

In this solution the author mentioned dbf.Table.export() but I cannot find it from current version of lib.

Asked By: mtwhitecamel

||

Answers:

To create a table:

import dbf

new_table = dbf.Table('new_file_name.dbf', 'field1 C(10); field2 N(5,2)')

and to write records to it (or any open table in read/write mode):

new_table.open(dbf.READ_WRITE)

# using tuples
new_table.append(('a value', 27.31))

# using a dictionary
new_table.append({'field1':'a value', 'field2': 27.31})
Answered By: Ethan Furman
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.