2023年12月22日发(作者:)
value=e('0','').lstrip()ifvalue=='':value=0elifdeci:value=l(value)else:value=int(value)eliftyp=='D':y,m,d=int(value[:4]),int(value[4:6]),int(value[6:8])value=(y,m,d)eliftyp=='L':value=(valuein'YyTt'and'T')or(valuein'NnFf'and'F')or'?'eliftyp=='F':value=float(value)(value)yieldresultdefdbfwriter(f,fieldnames,fieldspecs,records):""" Return a string suitable for writing directly to a binary dbf f should be open for writing in a binary ames should be no longer than ten characters and not include pecs are in the form (type, size, deci) wheretype is one of:C for ascii character dataM for ascii character memo data (real memo fields not supported)D for datetime objectsN for ints or decimal objectsL for logical values 'T', 'F', or '?'size is the field widthdeci is the number of decimal places in the provided decimal objectRecords can be an iterable over the records (sequences of field values)."""# header infover=3now=()
yr,mon,day=-1900,,rec=len(records)numfields=len(fieldspecs)lenheader=numfields*32+33lenrecord=sum(field[1]forfieldinfieldspecs)+1hdr=('(hdr)# field specsforname,(typ,size,deci)(fieldnames,fieldspecs):name=(11,'x00')fld=('<11sc4xBB14x',name,typ,size,deci)(fld)# ('r')# recordsforrecordinrecords:(' ')# deletion flagfor(typ,size,deci),(fieldspecs,record):iftyp=="N":value=str(value).rjust(size,' ')eliftyp=='D':value=me('%Y%m%d')eliftyp=='L':value=str(value)[0].upper()else:value=str(value)[:size].ljust(size,' ')assertlen(value)==(value)# End of ('x1A')# -------------------------------------------------------# Example callsif__name__=='__main__':importsys,csv
fromcStringIOimportStringIOfromoperatorimportitemgetter# Read a databasefilename='/pydev/databases/'iflen()==2:filename=[1]f=open(filename,'rb')db=list(dbfreader(f))()forrecordindb:printrecordfieldnames,fieldspecs,records=db[0],db[1],db[2:]# Alter the databasedelrecords[4](key=itemgetter(4))# Remove a fielddelfieldnames[0]delfieldspecs[0]records=[rec[1:]forrecinrecords]# Create a new DBFf=StringIO()dbfwriter(f,fieldnames,fieldspecs,records)# Read the data back from the new DBFprint'-'*(0)forlineindbfreader(f):()# Convert to CSVprint'.'*20f=StringIO()(f).writerow(fieldnames)(f).writerows(records)ue()
()# Example Output"""['ORDER_ID', 'CUSTMR_ID', 'EMPLOY_ID', 'ORDER_DATE', 'ORDER_AMT'][('C', 10, 0), ('C', 11, 0), ('C', 11, 0), ('D', 8, 0), ('N', 12, 2)]['10005 ', 'WALNG ', '555 ', (1995, 5, 22), Decimal("173.40")]['10004 ', 'BMARK ', '777 ', (1995, 5, 18), Decimal("3194.20")]['10029 ', 'SAWYH ', '777 ', (1995, 6, 29), Decimal("97.30")]['10013 ', 'RITEB ', '777 ', (1995, 6, 2), Decimal("560.40")]['10024 ', 'RATTC ', '444 ', (1995, 6, 21), Decimal("2223.50")]['10018 ', 'RATTC ', '444 ', (1995, 6, 12), Decimal("1076.05")]['10025 ', 'RATTC ', '444 ', (1995, 6, 23), Decimal("185.80")]['10038 ', 'OLDWO ', '111 ', (1995, 7, 14), Decimal("863.96")]['10002 ', 'MTIME ', '333 ', (1995, 5, 16), Decimal("731.80")]['10007 ', 'MORNS ', '444 ', (1995, 5, 24), Decimal("1405.00")]['10026 ', 'MORNS ', '555 ', (1995, 6, 26), Decimal("17.40")]['10030 ', 'LILLO ', '111 ', (1995, 7, 3), Decimal("909.91")]['10022 ', 'LAPLA ', '111 ', (1995, 6, 19), Decimal("671.50")]['10035 ', 'HIGHG ', '111 ', (1995, 7, 11), Decimal("1984.83")]['10033 ', 'FOODG ', '333 ', (1995, 7, 6), Decimal("3401.32")]--------------------['CUSTMR_ID', 'EMPLOY_ID', 'ORDER_DATE', 'ORDER_AMT'][('C', 11, 0), ('C', 11, 0), ('D', 8, 0), ('N', 12, 2)]['MORNS ', '555 ', (1995, 6, 26), Decimal("17.40")]['SAWYH ', '777 ', (1995, 6, 29), Decimal("97.30")]['WALNG ', '555 ', (1995, 5, 22), Decimal("173.40")]['RATTC ', '444 ', (1995, 6, 23), Decimal("185.80")]['RITEB ', '777 ', (1995, 6, 2), Decimal("560.40")]['LAPLA ', '111 ', (1995, 6, 19), Decimal("671.50")]['MTIME ', '333 ', (1995, 5, 16), Decimal("731.80")]['OLDWO ', '111 ', (1995, 7, 14), Decimal("863.96")]['LILLO ', '111 ', (1995, 7, 3), Decimal("909.91")]['RATTC ', '444 ', (1995, 6, 12), Decimal("1076.05")]['MORNS ', '444 ', (1995, 5, 24), Decimal("1405.00")]
['HIGHG ', '111 ', (1995, 7, 11), Decimal("1984.83")]['BMARK ', '777 ', (1995, 5, 18), Decimal("3194.20")]['FOODG ', '333 ', (1995, 7, 6), Decimal("3401.32")]....................CUSTMR_ID,EMPLOY_ID,ORDER_DATE,ORDER_AMTMORNS ,555 ,1995-06-26,17.40SAWYH ,777 ,1995-06-29,97.30WALNG ,555 ,1995-05-22,173.40RATTC ,444 ,1995-06-23,185.80RITEB ,777 ,1995-06-02,560.40LAPLA ,111 ,1995-06-19,671.50MTIME ,333 ,1995-05-16,731.80OLDWO ,111 ,1995-07-14,863.96LILLO ,111 ,1995-07-03,909.91RATTC ,444 ,1995-06-12,1076.05MORNS ,444 ,1995-05-24,1405.00HIGHG ,111 ,1995-07-11,1984.83BMARK ,777 ,1995-05-18,3194.20FOODG ,333 ,1995-07-06,3401.32"""


发布评论