#-*-Coding:utf-8-*-ImportStruct,csv,datetime class dbf_operator(): @staticmethod def Shhq_dbf_reader(f):Numrec, Lenheader = Struct.unpack (' <xxxxlh22x ', F.read ( +)) Numfields = (Lenheader- -) // +Fields = [] forFieldnoinchXrange (numfields): Name, Typ, size, Deci = Struct.unpack (' <11sc4xbb14x ', F.read ( +)) name = Name.replace (' + ',"')# Eliminate Nuls from stringFields.Append ((name, Typ, size, deci)) Terminator = F.read (1)assertTerminator = =' \ r 'Fields.insert (0, (' Deletionflag ',' C ',1,0) FMT ="'. Join (['%ds '% fieldinfo[2] forFieldInfoinchFields]) Fmtsiz = struct.calcsize (FMT) result = []# (STOCK,S3) #suspend_result = [] forIinchXrange (numrec): Record = Struct.unpack (FMT, F.read (fmtsiz)) flag = record[0] Stock = record[1]ifStock = ="000000":Pass elifstock[0] ==' 6 ' andFlag = ="': Result.append ((record[1],record[3]))returnResult@staticmethod def Szhq_dbf_reader(f):Numrec, Lenheader = Struct.unpack (' <xxxxlh22x ', F.read ( +)) Numfields = (Lenheader- -) // +Fields = [] forFieldnoinchXrange (numfields): Name, Typ, size, Deci = Struct.unpack (' <11sc4xbb14x ', F.read ( +)) name = Name.replace (' + ',"')# Eliminate Nuls from stringFields.Append ((name, Typ, size, deci)) Terminator = F.read (1)assertTerminator = =' \ r 'Fields.insert (0, (' Deletionflag ',' C ',1,0) FMT ="'. Join (['%ds '% fieldinfo[2] forFieldInfoinchFields]) Fmtsiz = struct.calcsize (FMT) result = []# (STOCK,S3) forIinchXrange (numrec): Record = Struct.unpack (FMT, F.read (fmtsiz)) flag = record[0] Stock = record[1]ifStock = ="000000":Pass elif(stock[0] ==' 0 ' orstock[:3] ==' + ') andFlag = ="': Result.append ((record[1],record[3]))returnResult
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
DBF file using Python read program