Monthly Archives: October 2017

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',, 3, 1))])
OrderedDict([('NAME', 'Bob'), ('BIRTHDATE',, 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'])
>>> print(table.records[0]['NAME'])

 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)
    ("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",, 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

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"


What is milter?

Every one gets tons of email these days. This includes emails about super duper offers from amazon to princess and wealthy businessmen trying to offer their money to you from some African country that you have never heard of. In all these emails in your inbox there lies one or two valuable emails either from your friends, bank alerts, work related stuff. Spam is a problem that email service providers are battling for ages. There are a few opensource spam fighting tools available like SpamAssasin or SpamBayes.

What is milter ?

Simply put – milter is mail filtering technology. Its designed by sendmail project. Now available in other MTAs also. People historically used all kinds of solutions for filtering mails on servers using procmail or MTA specific methods. The current scene seems to be moving forward to sieve. But there is a huge difference between milter and sieve. Sieve comes in to picture when mail is already accepted by MTA and had been handed over to MDA. On the other hand milter springs into action in the mail receiving part of MTA. When a new connection is made by remote server to your MTA, your MTA will give you an opportunity to accept of reject the mail every step of the way from new connection, reception of each header, and reception of body.

milter protocol various stages

The above picture depicts simplified version of milter protocol working. Full details of milter protocol can be found here  . Not only filtering; using milter, you can also modify message or change headers.


If you want to get started in C you can use libmilter.  For Python you have couple of options:

  1. pymilter –
  2. txmilter –

Postfix supports milter protocol. You can find every thing related to postfix’s milter support in here –


I found sieve to be rather limited. It doesn’t offer too many options to implement complex logic. It was purposefully made like that. Also sieve starts at the end of mail reception process after mail is already accepted by MTA.

Coding milter program in your favorite programming language gives you full power and allows you to implement complex , creative stuff.


When writing milter programs take proper care to return a reply to MTA quickly. Don’t do long running tasks in milter program when the MTA is waiting for reply. This will have crazy side effects like remote parties submitting same mail multiple time filling up your inbox.