Next / Previous / Contents / Shipman's homepage

90. The dbfpy package for reading dBASE V files

The dbfstruct and dbflatten scripts use a public-domain Python module for reading dBASE V files. See the Python Cheese Shop page for more information about this module.

Here are links to the sources for the five modules in this package:

The dbfpy package has no documentation; one must read the code to learn how to use it. The package has many more features we need, e.g., the ability to create new databases and add records. We need only read-only access to .dbf files.

Here is a brief summary of the basic features of the package. Import the package like this:

import dbf

90.1. Dbf: Database object

Class Dbf represents an entire .dbf file. Its interface:

dbf.Dbf(fileName, readOnly=False, new=False)
fileName

Name of the .dbf file to be read.

readOnly

If true, allows changes to the file.

new

If true, creates a new file with the given name.

Some relevant attributes of a Dbf instance:

.header

A DbfHeader object containing general information about the file; see Section 90.2, “DbfHeader: Database header object”.

.recordCount

Number of records in the file.

.fieldNames

List of field names, in record order.

.fieldDefs

List of DbfFieldDef objects that define the columns of the database; see Section 90.3, “DbfFieldDef: Field definition object”.

To retrieve a record from the file, you can use either iteration or indexing. To iterate over the records in a Dbf object D:

for r in D: ...

This loop would set r to each record in turn. You can also ask for the Nth record in the file by using the expression “D[N]”.

With either method, the returned record is an instance of class DbfRecord; see Section 90.4, “DbfRecord: Record object”.