简体   繁体   中英

Converting .CSV to .DBF(dBASEIII) VFP 6.0, everything becomes memo field

I'm trying to convert excel files to dbf(dBASEIII) using python and my current process is:

  1. Use xlrd to convert the excel file to a .csv
  2. I take the headers off of the .csv and I use
  3. Take the newly made .csv and use the dbf module ( https://pypi.python.org/pypi/dbf ) to convert to dbf

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'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM