我正在尝试使用 python 将 excel 文件转换为 dbf(dBASEIII),我当前的过程是:
我从 csv 文件中删除标题并运行以下命令:
table = dbf.from_csv(origfname, filename='test.dbf', field_names=headers, dbf_type='db3')
截至目前,当流程结束时,所有字段都成为备注字段,我如何将它们变成 Char、Date、Number 等字段?
最佳答案
from_csv
方法仅用于转储到备注字段。
如果你想要更多的控制,那么你应该跳过 csv
从xls
开始至dbf
, 使用 xlrd
和 dbf
.
这将需要更多的工作:您必须首先使用适当的字段创建表,但随后只需遍历 xls 表并写入 dbf 表即可。
所以是这样的:
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'
关于python - 将 .CSV 转换为 .DBF(dBASEIII) VFP 6.0,一切都变成了备忘录字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70814643/