将.CSV转换为.DBF(dBASEIII)VFP 6.0,一切都变成备忘录字段

时间:2016-03-10 01:26:55

标签: python visual-foxpro foxpro dbf dbase

我尝试使用python将excel文件转换为dbf(dBASEIII),我当前的流程是:

  1. 使用xlrd将excel文件转换为.csv
  2. 我从.csv中取出标题,然后使用
  3. 获取新制作的.csv并使用dbf模块(https://pypi.python.org/pypi/dbf)转换为dbf
  4. 我从csv文件中删除了标题,然后运行以下命令:

    table = dbf.from_csv(origfname, filename='test.dbf', field_names=headers, dbf_type='db3')
    

    截至目前,当进程结束时,所有字段都成为备注字段,如何将它们变为字符,日期,数字等字段?

1 个答案:

答案 0 :(得分:2)

from_csv方法仅用于转储到备注字段中。

如果您想要更多控制权,那么您应该跳过csv步骤,并使用xlsdbfxlrd转到dbf

这将需要更多的工作:您必须首先使用适当的字段创建表,但是这是一个简单的事情,即迭代xls表并写入dbf表。 / p>

这样的事情:

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()

要自动生成字段名称和列类型,您需要循环浏览电子表格的前几行以获取标题名称和值类型。这是我迭代的示例数据行:

<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'
相关问题