pythondbf

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


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.


Solution

  • 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})