Read DBF Files with Python

2.0.7 · active · verified Fri Apr 10

dbfread is a pure Python library designed to read DBF files (used by databases like dBase, Visual FoxPro, and FoxBase+). It processes the data and returns it as native Python data types, making it ideal for batch jobs and one-off scripts. The current version is 2.0.7, and while stable, it is not actively developed, with its last update being in late 2016.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to open a DBF file and iterate through its records. It also shows how to load all records into memory if random access by index is required. It assumes a 'people.dbf' file exists for demonstration.

import datetime
from dbfread import DBF

# For this example to run, ensure 'people.dbf' exists in the same directory
# or provide a full path. A dummy 'people.dbf' might look like:
# NAME      BIRTHDATE
# Alice     1987-03-01
# Bob       1980-11-12

# Open the DBF file and iterate through records (streaming, memory-efficient)
print("Streaming records:")
try:
    for record in DBF('people.dbf'):
        print(record)
except Exception as e:
    print(f"Error reading DBF: {e}. Please ensure 'people.dbf' exists in the current directory.")

# Load all records into memory for random access (if needed)
print("\nLoading all records into memory for random access:")
try:
    table = DBF('people.dbf', load=True)
    print(f"First record name: {table.records[0]['NAME']}")
    print(f"Total records: {len(table.records)}")
except Exception as e:
    print(f"Error reading DBF with load=True: {e}. Please ensure 'people.dbf' exists.")

view raw JSON →