Playing around with arcane tools to read those pesky DBF files (with
memo fields), like floating wine barrels cast off the sinking VFP
ship.
http://pypi.python.org/pypi/dbf
http://packages.python.org/dbf/dbf-module.html
Me threading on comp.lang.python:
http://bit.ly/e547Za
xlwt and xlrt for reading and writing Excel data, without needing
Excel, are also making Python attractive
Here's a code fragment giving the idea:
from os import path
from time import time
import dbf
def test2():
thepath = path.normpath(<< file path >>)
thefile = "shiptemp.dbf"
pathfile = path.join(thepath, thefile)
thedbf = dbf.VfpTable(pathfile)
header_style = easyxf('font: name Arial, bold True, height 200;')
book = Workbook()
sheet1 = book.add_sheet('Sheet 1')
for (i, name) in enumerate(thedbf.field_names):
sheet1.write(0, i, name, header_style)
for (i, thecol) in enumerate(thedbf.structure()):
name, thetype = str(thecol).split()
thelen, thedec = 0,0
if "(" in thetype:
if "," in thetype:
thelen, thedec = [int(k) for k in
thetype[2:-1].split(",")]
else:
thelen, thedec = int(thetype[2:-1]), 0
thetype = thetype[0]
if thetype == "M":
thelen = 100
elif thelen == 0:
thelen = 1
colwidth = max(len(name), int(thelen))
# print colwidth
sheet1.col(i).width = colwidth * 310
thedbf.top()
messages = []
for row in range(1, len(thedbf)):
record = thedbf.next()
for col in range(len(thedbf.field_names)):
try:
sheet1.row(row).write(col, record[col])
except Exception:
pass
book.save("C:\\Documents and Settings\\HP_Administrator\\My
Documents\\Visual FoxPro Projects\\shiptemp.xls")
thedbf.close()
if __name__ == "__main__":
start = time()
test2()
end = time()
print end - start
At the moment I'm unable to get the 1500 record x 156 column table
(with memos) to stay in
memory even with the disk file closed, which I understand is a feature
of this package
http://pypi.python.org/pypi/dbf/
thetable = dbf.VfpTable(thefile)
thetable[1]
thetable.close(keep_table=True, keep_memos=True)
Traceback (most recent call last):
File "<pyshell#105>", line 1, in <module>
thetable.close(keep_table=True, keep_memos=True)
File "C:/Python26/Lib/site-packages\dbf\tables.py", line 1141, in
close
for record in yo:
File "C:/Python26/Lib/site-packages\dbf\tables.py", line 676, in
next
record = yo._table[yo._index]
File "C:/Python26/Lib/site-packages\dbf\tables.py", line 817, in
__getitem__
return yo._table[yo._index[value]]
File "C:/Python26/Lib/site-packages\dbf\tables.py", line 655, in
__getitem__
yo._meta.dfd.seek(location)
AttributeError: 'NoneType' object has no attribute 'seek'
Same error with:
Python 2.6 / Win7
Kirby