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