I'm trying to convert excel files to dbf(dBASEIII) using python and my current process is:
I remove the headers from the csv file and I run the following:
table = dbf.from_csv(origfname, filename='test.dbf', field_names=headers, dbf_type='db3')
As of now, when the process is over all the fields become memo fields, how do I make them into Char, Date, Number, etc... fields?
The from_csv
method is only intended to dump into Memo fields.
If you want more control then you should skip the csv
step and go from xls
to dbf
, using xlrd
and dbf
.
It will take a little more work: You'll have to create the table first with the appropriate fields, but then it's a simple matter of iterating through the xls table and writing to the dbf table.
So something like this:
some_table = Dbf.Table(
'whatever.dbf',
'name C(25); age N(3); history M',
codepage='cp1252',
)
# get data from xlrd (specifics are not correct, psuedo-code only)
...
data = []
for row in sheet:
for cell in row:
data.append(cell.value)
# back to real code
some_table.append(tuple(data))
# all data has been transferred
some_table.close()
To automatically generate the field names and column types you would need to cycle through the first few rows of the spreadsheet to get the header names and value types. Here is an example data row I iterated through:
<type 'unicode'> u'PROD'
<type 'unicode'> u'cclark'
<type 'float'> 4.0
<type 'float'> 99.0
<type 'unicode'> u'501302'
<type 'unicode'> u'244026'
<type 'int'> 1
<type 'float'> 42444.0
<type 'str'> ''
<type 'unicode'> u'AB'