Read DBF Files with Python
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
- breaking The functions `dbfread.open()` and `dbfread.read()` are deprecated since version 2.0 and are scheduled for removal in version 2.2. Direct list-like access on the `DBF` object (e.g., `table[0]`) no longer works by default since version 1.1.0.
- gotcha Character encoding issues are common with DBF files. While `dbfread` attempts to detect the encoding, it may fall back to `ASCII` (since 1.1.0) or `latin1` (older versions), leading to `UnicodeDecodeError` or incorrect characters.
- gotcha Memo files (`.fpt`, `.dbt`) associated with DBF files can cause issues if missing or if file casing on non-Windows systems (e.g., Linux) prevents detection (as Windows filenames are case-preserving).
- gotcha DBF field names are typically uppercase. When accessing fields as dictionary keys, this might be inconvenient in Python where lowercase conventions are common.
Install
-
pip install dbfread
Imports
- DBF
from dbfread import DBF
- open
from dbfread import DBF
- read
from dbfread import DBF
Quickstart
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.")