Python module for Dbase files?
William Dandreta
wjdandreta at worldnet.att.net
Fri Jun 23 03:43:16 EDT 2000
Hi Anders,
>Why don't you set up a Linux server and a TCP/IP net with DOS clients.
>Then you could have the database on the server and you would have much
>more to choose from!
That would be a good idea but we are not using plain DOS. It is Concurrent
DOS, amultitasking/mulituser DOS system and each user has a terminal. It is
a legacy system that is no longer being upgraded. It does not support
networking.
>>
>It's probably nothing wrong with xDB, but I think it's rather a big
>task to port it to DOS and create a Python Wrapper. So maybe there are
>other solutions that are better?
I don't think it will be as difficult as you imagine. I have documentation
on the dbase file structure. I wrote a simple Python program today that
takes the data in a Dbase file an converts it to a comma delimited ASCII
file. (A copy is below, I haven't finished all the data types yet.)
The only part I haven't a clue about is the indexing. Our needs are limited.
If I can add to the database, update indexes and access data using indexes,
I would be satisfied. So, I would only need to port a small part of it.
-------------------------------------------
# converts .dbf file to comma delimited ascii.
from struct import unpack
from sys import exit
DBFfile = 'tier_5.dbf'
DBF = open(DBFfile,'rb')
hdra = DBF.read(32)
numRecs,hdrSize,recSize = unpack('lhh',hdra[4:12])
hdrbSize = hdrSize-32
hdrb = DBF.read(hdrbSize)
numFields = (hdrbSize-1)/32
if '\x0D' <> hdrb[-1]:
print 'Cannot read DBF file, header is corrupted.'
exit(1)
line = ''
varFields = {}
for i in range(11,32*numFields+11,32):
if (hdrb[i] == 'C') or (hdrb[i] == 'N'):
line = line + '%s, '
varFields[i] = ord(hdrb[i+5])
elif hdrb[i] == 'D':
pass #date stuff here
elif hdrb[i] == 'L':
pass #logic stuff here
else:
line = line + 'NOT A TEXT FIELD ,'
line = line[:-2] + '\n'
txtFile = DBFfile[:-3]+'txt'
txt = open(txtFile,'w')
for i in range(numRecs):
record = DBF.read(recSize)
vf = ()
j = 0
k = 1 #first byte is a tag
while j < 32*numFields:
fieldSize = varFields[11+j]
vf = vf + (record[k:k+fieldSize],)
k = k + fieldSize
j = j + 32
txt.write(line % vf)
--------------------------------------------
Bill
More information about the Python-list
mailing list