Dbase / foxpro files

Ethan Furman efurman at admailinc.com
Fri May 16 14:46:54 EDT 2008


Johny wrote:

>Is there a module for reading/modifing db files from Python?
>Thanks for help
>B.
>--
>http://mail.python.org/mailman/listinfo/python-list
>  
>
I'm switching my company's software base over from FoxPro 6 to Python.  
As part of that effort I have written (and am still enhancing :) a 
dbf.py module which reads/writes both dBase III and FoxPro tables. 

Index files are not supported, but the table can be sorted by any 
combination of fields after being opened. 

dBase III table/memo support is complete, but FoxPro field types 
Currency, Double, General, and Picture are not supported and those 
fields are stripped out when opened, and will not be in any saved 
version of that table.  At this point, the dbf file itself is read into 
memory, all updates are held in memory, and the table is only written to 
disk when the Save method is called.

Record navigation can be sequential or random, and Top, Bottom, Next, 
and Previous are supported.

Searching is supported, using (or not) deleted records is supported, 
adding and deleting fields is supported, saving as a csv file, and more.

Let me know if you'd like the module.  Hope this helps.

Sample session follows...

 >>> import dbf
 >>> table = dbf.DbfTable('newtable','name C(10), age N(3.0), wisdom M')
 >>> print table

        Table: newtable.dbf
        Type:  dBase III Plus
        Last updated:  2008-05-16
        Record count:  0
        Field count:   3
        Record length: 24

        --Fields--
        name C(10)
        age N(3.0)
        wisdom M(10)

 >>> table.Append()
 >>> table.name = 'Ethan'
 >>> table.age = 37
 >>> table.wisdom = 'Python rules!'
 >>> table.Scatter()
{'age': 37, 'name': 'Ethan', 'wisdom': 'Python rules!'}
 >>> table.GetField('name')
'Ethan'
 >>> table[0]
 Ethan      37         1
 >>> record = table[0]
 >>> record.name
'Ethan'
 >>> record.wisdom
'Python rules!'
 >>> record.age = 40
 >>> record
 Ethan      40         1
 >>> table[0]
 Ethan      40         1
 >>> table.Append({'name':'Lori', 'age':45, 'wisdom':'happy gardens make 
a happy wife'})
 >>> table.Scatter()
{'age': 45, 'name': 'Lori', 'wisdom': 'happy gardens make a happy wife'}
 >>> for record in table:
...   print record.name, record.wisdom
...
Ethan Python rules!
Lori happy gardens make a happy wife
 >>>





More information about the Python-list mailing list