Hi,
I want ot create a dbf file, the file is created,it can be opened by Excel
but it cannot be opened by Access. Where is the error in my script.
My script is as follows:
#!/opt/bin/python2.3
import struct, datetime,itertools,time
def dbfwriter(f, fieldnames, fieldspecs, records):
"""
Return a string suitable for writing directly to a binary dbf file.
File f should be open for writing in a binary mode.
Fieldnames should be no longer than ten characters and not include
\x00.
Fieldspecs are in the form (type, size, deci) where
type is one of:
C for ascii character data
&n! bsp; M for ascii character memo data (real memo fields not
supported)
D for datetime objects
N for ints or decimal objects
L for logical values 'T', 'F', or '?'
size is the field width
deci is the number of decimal places in the provided decimal
object
Records can be an iterable over the records (sequences of field
values).
\x00.
Fieldspecs are in the form (type, size, deci) where
type is one of:
C for ascii character data
&n! bsp; M for ascii character memo data (real memo fields not
supported)
D for datetime objects
N for ints or decimal objects
L for logical values 'T', 'F', or '?'
size is the field width
deci is the number of decimal places in the provided decimal
object
Records can be an iterable over the records (sequences of field
values).
"""
# header info
ver = 3
now = datetime.datetime.now()
yr, mon, day = now.year-1900, now.month, now.day
numrec = len(records)
numfields = len(fieldspecs)
lenheader = numfields * 32 + 33
# lenrecord = sum(field[1] for field in fieldspecs) + 1
 ! ; num = 0
for field in fieldspecs :
num = num + int(field[1])
# header info
ver = 3
now = datetime.datetime.now()
yr, mon, day = now.year-1900, now.month, now.day
numrec = len(records)
numfields = len(fieldspecs)
lenheader = numfields * 32 + 33
# lenrecord = sum(field[1] for field in fieldspecs) + 1
 ! ; num = 0
for field in fieldspecs :
num = num + int(field[1])
lenrecord = num + 1
hdr = struct.pack('<BBBBLHH20x', ver, yr, mon, day, numrec, lenheader, lenrecord)
f.write(hdr)
f.write(hdr)
# field specs
for name, (typ, size, deci) in itertools.izip(fieldnames,
fieldspecs):
# name = name.ljust(11, '\x00')
name = name.ljust(11)
fld = struct.pack('<11sc4xBB14x', name, typ, size, deci)
f.write(fld)
for name, (typ, size, deci) in itertools.izip(fieldnames,
fieldspecs):
# name = name.ljust(11, '\x00')
name = name.ljust(11)
fld = struct.pack('<11sc4xBB14x', name, typ, size, deci)
f.write(fld)
# terminator
f.write('\r')
f.write('\r')
# records
for record in records:
f.write(' ') # deletion flag
&n! bsp; for (typ, size, deci), value in itertools.izip(fieldspecs,
record):
if typ == "N":
# value = str(value).rjust(size, ' ')
value = str(value).rjust(size)
elif typ == 'D':
# value = value.strftime('%Y%m%d')
value = value
elif typ == 'L':
value = str(value)[0].upper()
else:
# value = str(value)[:size].ljust(size, ' ')
value = str(value)[:size].ljust(size)
&! nbsp; assert len(value) == size
f.write(value)
for record in records:
f.write(' ') # deletion flag
&n! bsp; for (typ, size, deci), value in itertools.izip(fieldspecs,
record):
if typ == "N":
# value = str(value).rjust(size, ' ')
value = str(value).rjust(size)
elif typ == 'D':
# value = value.strftime('%Y%m%d')
value = value
elif typ == 'L':
value = str(value)[0].upper()
else:
# value = str(value)[:size].ljust(size, ' ')
value = str(value)[:size].ljust(size)
&! nbsp; assert len(value) == size
f.write(value)
# End of file
f.write('\x1A')
f.close()
f.write('\x1A')
f.close()
# -------------------------------------------------------
# Example calls
if __name__ == '__main__':
# Example calls
if __name__ == '__main__':
import sys, csv
from cStringIO import StringIO
# from operator import itemgetter
from cStringIO import StringIO
# from operator import itemgetter
# Create a new DBF
# f = StringIO()
# f = StringIO()
f = open('test.dbf','w')
fieldnames = ['CUSTOMER_ID','EMPLOY_ID','ORDER_DATE','ORDER_AMT']
fieldspecs = [('C',11,0),('C',11,0),('D',8,0),('N',12,2)]
records = [['MORNS','555','19950626','17.40'],\
['SAWYH','777','19950629','97.30'],\
['WALNG','555','19950522','173.40']]
Any help is appreciated
fieldnames = ['CUSTOMER_ID','EMPLOY_ID','ORDER_DATE','ORDER_AMT']
fieldspecs = [('C',11,0),('C',11,0),('D',8,0),('N',12,2)]
records = [['MORNS','555','19950626','17.40'],\
['SAWYH','777','19950629','97.30'],\
['WALNG','555','19950522','173.40']]
dbfwriter(f, fieldnames, fieldspecs, records)
Any help is appreciated
Thanks,
William
_______________________________________
YM - 離線訊息
就算你沒有上網,你的朋友仍可以留下訊息給你,當你上網時就能立即看到,任何說話都冇走失。
http://messenger.yahoo.com.hk
-- http://mail.python.org/mailman/listinfo/python-list