Dbf file in python

What is DBF file? How to read it in linux and python?

What is DBF files ?

A DBF file is a standard database file used by dBASE, a database management system application. It organises data into multiple records with fields stored in an array data type. DBF files are also compatible with other “xBase” database programs, which became an important feature because of the file format’s popularity.

Tools which can read or open DBF files

Below are list of program which can read and open dbf file.

  • Windows
    1. dBase
    2. Microsoft Access
    3. Microsoft Excel
    4. Visual Foxpro
    5. Apache OpenOffice
    6. dbfview
    7. dbf Viewer Plus
  • Linux
    1. Apache OpenOffice
    2. GTK DBF Editor

How to read file in linux ?

“dbview” command available in linux, which can read dbf files.

Below code snippet show how to use dbview command.

[lalit : temp]₹ dbview test.dbf 
Name       : John
Surname    : Miller
Initials   : JM
Birthdate  : 19800102

Name       : Andy
Surname    : Larkin
Initials   : AL
Birthdate  : 19810203

Name       : Bill
Surname    : Clinth
Initials   : 
Birthdate  : 19820304

Name       : Bobb
Surname    : McNail
Initials   : 
Birthdate  : 19830405

[lalit : temp]₹ 

 How to read it using python ?

dbfread” is the library available in python to read dbf files. This library reads DBF files and returns the data as native Python data types for further processing.

dbfread requires python 3.2 or 2.7.  dbfread is a pure python module, so doesn’t depend on any packages outside the standard library.

You can install library by the command below.

pip install dbfread

The below code snippet can read dbf file and retrieve data as python dictionary.

>>> from dbfread import DBF

>>> for record in DBF('people.dbf'):
...     print(record)

<strong>Out Put</strong>
OrderedDict([('NAME', 'Alice'), ('BIRTHDATE', datetime.date(1987, 3, 1))])
OrderedDict([('NAME', 'Bob'), ('BIRTHDATE', datetime.date(1980, 11, 12))])

You can also use the with statement:

with DBF('people.dbf') as table:
    ...

By default the records are streamed directly from the file.  If you have enough memory you can load them into a list instead. This allows random access

>>> table = DBF('people.dbf', load=True)
>>> print(table.records[1]['NAME'])
Bob
>>> print(table.records[0]['NAME'])
Alice

 How to Write content in DBF file using python ?

dbfpy is a python-only module for reading and writing DBF-files.  dbfpy can read and write simple DBF-files.

You can install it by using below command

pip install dbfpy

The below example shows how to create dbf files and write records in to it.

import datetime
from mx import DateTime
from dbfpy import dbf

## create empty DBF, set fields

db = dbf.Dbf("test.dbf", new=True)
db.addField(
    ("NAME", "C", 15),
    ("SURNAME", "C", 25),
    ("INITIALS", "C", 10),
    ("BIRTHDATE", "D"),
)

## fill DBF with some records

for name, surname, initials, birthdate in (
    ("John", "Miller", "JM", (1980, 1, 2)),
    ("Andy", "Larkin", "AL", datetime.date(1981, 2, 3)),
    ("Bill", "Clinth", "", DateTime.Date(1982, 3, 4)),
    ("Bobb", "McNail", "", "19830405"),
):
    
    rec = db.newRecord()
    rec["NAME"] = name
    rec["SURNAME"] = surname
    rec["INITIALS"] = initials
    rec["BIRTHDATE"] = birthdate
    rec.store()
db.close()

Also you can update a dbf file record using dbf module.

The below example shows how to update a record in a .dbf file.

db = dbf.Dbf("test.dbf")
rec = db[2]
rec["INITIALS"] = "BC"
rec.store()

 

Leave a Reply

Your email address will not be published. Required fields are marked *