Table data file DBF read and write operations

Source: Internet
Author: User
Tags assert first row unpack

ImportSYSImportCSVImportstructImportdatetimeImportdecimalImportItertools fromCstringioImportStringio fromoperatorImportItemgetterdefDbfreader (f):"""Returns an iterator to records in a Xbase DBF file.    The first row returned contains the field names.    The second row contains field specs: (Type, size, decimal places).    Subsequent rows contain the data records.    If a record is marked as deleted, it's skipped.    File should is opened for binary reads. """    #See DBF format spec at:    #Http://www.pgts.com.au/download/public/xbase.htm#DBF_STRUCTNumrec, Lenheader= Struct.unpack ('<xxxxlh22x', F.read (32)) Numfields= (lenheader-33)//32 Fields= []     forFieldnoinchxrange (numfields): Name, Typ, size, Deci= Struct.unpack ('<11sc4xbb14x', F.read (32)) name= Name.replace (' /',"')#eliminate Nuls from stringFields.Append ((name, Typ, size, deci))yield[Field[0] forFieldinchFields ]yield[Tuple (field[1:]) forFieldinchFields ] Terminator= F.read (1)    assertTerminator = ='\ r'Fields.insert (0, ('Deletionflag','C', 1, 0)) FMT="'. Join (['%ds'% fieldinfo[2] forFieldInfoinchFields ]) Fmtsiz=struct.calcsize (FMT) forIinchxrange (numrec): Record=Struct.unpack (FMT, F.read (fmtsiz))ifRECORD[0]! =' ':            Continue  #deleted recordresult = []         for(Name, Typ, size, deci), valueinchitertools.izip (Fields, record):ifName = ='Deletionflag':                Continue            ifTyp = ="N": Value= Value.replace (' /',"'). Lstrip ()ifValue = ="': Value=0elifDeci:value=Decimal. Decimal (value)Else: Value=int (value)elifTyp = ='D': Y, M, D= Int (value[:4]), int (value[4:6]), int (value[6:8]) value=datetime.date (Y, M, D)elifTyp = ='L': Value= (valueinch 'Yytt'  and 'T')or(Valueinch 'NNFF'  and 'F')or '?'            elifTyp = ='F': Value=Float (value) result.append (value)yieldresultdefDbfwriter (F, FieldNames, Fieldspecs, records):"""Return A string suitable for writing directly to a binary DBF file.    File F should is open for writing in a binary mode.    FieldNames should is no longer than ten characters and not include \x00.            Fieldspecs is in the form (type, size, deci) where type is a of:c for ASCII character data  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 are the number of decimal places in the provided decimal object Records can be a It    Erable over the records (sequences of field values). """    #Header Infover = 3 Now=Datetime.datetime.now () yr, Mon, day= now.year-1900, Now.month, Now.day Numrec=Len (Records) Numfields=Len (fieldspecs) Lenheader= Numfields * 32 + 33Lenrecord= SUM (field[1] forFieldinchFIELDSPECS) + 1HDR= Struct.pack ('<bbbblhh20x', ver, yr, Mon, day, Numrec, Lenheader, Lenrecord) f.write (HDR)#Field Specs     forName, (Typ, size, deci)inchItertools.izip (FieldNames, fieldspecs): Name= Name.ljust (11,'\x00') FLD= Struct.pack ('<11sc4xbb14x', Name, Typ, size, Deci) f.write (FLD)#TerminatorF.write ('\ r')    #Records     forRecordinchRecords:f.write (' ')#Deletion flag         for(Typ, size, deci), valueinchItertools.izip (Fieldspecs, record):ifTyp = ="N": Value= str (value). Rjust (Size,' ')            elifTyp = ='D': Value= Value.strftime ('%y%m%d')            elifTyp = ='L': Value=Str (value) [0].upper ()Else: Value= str (value) [: Size].ljust (Size,' ')            assertLen (value) = =size F.write (value)#End of FileF.write ('\x1a')    ################################################################################## #3filename ='e:/update/shp/test.dbf'F= open (filename,'RB') DB=list (Dbfreader (f)) F.close () forRecordinchDB:PrintRecord## # # FieldNames is first row means fieldname,fieldspecs are second row means Fieldtype,records is afterrows means Recor DSFieldNames, fieldspecs, records = Db[0], db[1], db[2:]#Remove a fielddelFieldnames[0]delFieldspecs[0]records= [Rec[1:] forRecinchRecords]#Create a new DBFfilename1 ='e:/update/shp/test1.dbf'F1= Open (filename1,'wb+') Dbfwriter (F1, FieldNames, Fieldspecs, Records)#Read the data back from the new DBFPrint '-'* 50f1.seek (0) forLineinchDbfreader (F1):Printlinef1.close ()#Convert to CSVPrint '.'* 50filename1='E:/update/shp/test1.csv'F1= Open (filename1,'wb+') Csv.writer (F1). Writerow (FieldNames) csv.writer (F1). Writerows (Records)PrintF1.getvalue () f1.close ( )

Table data file DBF read and write operations

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.