Question
Provide a program to read a .DBF version 3 file and output the results to a .CSV file that can be read by MS-Excel. No
Provide a program to read a .DBF version 3 file and output the results to a .CSV file that can be read by MS-Excel. No library functions can be used other than the standard I/O routines. I can supply sample .DBF files. (pseudo code below)
# Python program to read a .DBF version 3 file
# then output the results to a .CSV file that can be read by MS-Excel.
#
# DBF file format explained at https://en.wikipedia.org/wiki/.dbf
#
# Input: .DBF file
filename = input("Please input the file name you would like to convert: ")
f = open(filename, 'rb') # read binary allows reading byte at a time
# The header is 32 bytes:
# byte 0 = version, bytes 1-3 = YMD
# bytes 4-7 = #records; bytes 8-9 = #headerlength; bytes 10-11 = #recordlength
# ignore bytes 12-31
#
dbase_ver = f.read(1)
last_yy = int.from_bytes(f.read(1), byteorder='little') % 100
last_mm = int.from_bytes(f.read(1), 'little')
last_dd = int.from_bytes(f.read(1), 'little')
numrec = int.from_bytes(f.read(4),'little') # get 4 byte integer as number of records
hdrlen = int.from_bytes(f.read(2),'little') # get header length, used to calc num of fields
reclen = int.from_bytes(f.read(2),'little') # get record length
num_fields = int(hdrlen/32)
f.read(20) # skip reserved section of 32 byte header
# after the 32 byte header above, is an array of "num_fields" field descriptors
# bytes 0-11=fieldname, byte 11=fieldtype, bytes12-15=ignore, byte 16=fieldlength, bytes17-31=ignore
#
# for each field (i.e. num_fields)
# -- save the following information
# read fieldname
# read fieldtype
# read fieldsize
#
# after all the field descriptors, there is one byte with x'0d'
# then the data records begin
# note: each record includes a byte at the beginning, ' '=valid, '*'=deleted
#
# skip the x'0d' byte
#
# for each record (i.e. numrec)
# read byte to determine if valid or deleted
# for each field
# read fieldsize bytes
# -- if not deleted, print this field
#
f.close()
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started