Skip to main content

read data from DBF files

Project description

Requires Python 2.7 or 3.2.

License: MIT

Latest version of the source code: http://github.com/olemb/dbfread/

Example

>>> import dbfread
>>> table = dbfread.read('people.dbf')

>>> for rec in table:
...     print(rec['NAME'], rec['BIRTHDAY'])
Alice 1987-03-01
Bob 1980-11-12

>>> for rec in table:
...     print(rec)
{u'NAME' : u'Alice', u'BIRTHDAY' : datetime.date(1987, 3, 1)}
{u'NAME' : u'Bob', 'uBIRTHDAY' : datetime.date(1980, 11, 12)}

All records will be read into memory. The Table class is a subclass of list, so you can use all the normal list operations on it.

Deleted records are available in table.deleted.

Status

This code in various incarnations has been in production at the University of Tromsø since 2001. The current version reads all data generated by Telemator (http://www.telemator.no/), which uses Visual Foxpro (as far as I can tell) and employs a wide range of data types.

The library has not been widely tested on other data, but I intend for it to be able to read any DBF file. If you have a file it can’t read, or find a bug, I’d love to hear from you.

Installing

Python 2:

sudo python setup.py install

Python 3:

sudo python3 setup.py install

Supported field types

:

Field type

Converted to

0

flags

int

C

text

unicode string

D

date

datetime.date or None

F

float

float or None

I

integer

int or None

L

logical

True, False or None

M

memo

unicode string (memo type) or byte string (picture and object type)

N

numeric

int, float or None

T

time

datetime.datetime

Options

By default, dbfread will try to guess the character encoding from the language_driver byte. If it can’t guess the encoding it uses “latin1”. You can override the encoding with the option:

encoding='latin1'

You can lower field names with lowernames=True:

>>> table = dbfread.read('people.dbf',
                         lowernames=True)
>>> for rec in table:
...     print(rec['name'], rec['birthday'])

The recfactory option takes any callable which accepts a list of `(name, value)` tuples, for example:

recfactory=collections.OrderedDict

One last option. By default, dbfread will assume that you’ve copied the DBF files from a windows file system, and that the file name casing is all scrambled. Thus, it will treat `People.FPT` as the same file as `PEOPLE.fpt`. You can turn off this behaviour with:

ignorecase=False

There is also an “undocumented” option, which I use mostly for debugging:

raw=True   # Returns all data values as raw bytestrings

Table attributes

The table object has a lot of attributes that can be useful for introspection. Some simple ones:

>>> table.name
'people'

>>> table.date
datetime.date(2012, 7, 11)

>>> table.encoding
'cp1252'

A list of field names can be used for producing CSV files, for example:

>>> table.field_names
[u'NAME', u'BIRTHDAY']

The file header and field headers are namedtuples:

>>> table.header
DBFHeader(dbversion=48, year=12, month=7, day=11, numrecords=555,
headerlen=2408, recordlen=632, reserved1=0, incomplete_transaction=0,
encryption_flag=0, free_record_thread=0, reserved2=0, reserved3=0,
mdx_flag=3, language_driver=3, reserved4=0)

>>> table.fields
[DBFField(name=u'NAME', type=u'C', address=1, length=25, decimal_count=0,
reserved1=0, workarea_id=0, reserved2=0, reserved3=0, set_fields_flag=0,
reserved4='\x00\x00\x00\x00\x00\x00\x00', index_field_flag=0),
... etc. ...]

dbf2sqlite

A tool is included in the `examples` directory to convert DBF into sqlite, for example:

dbf2sqlite -o stamnett.sqlite kabreg.dbf endreg.dbf

This will create one table for each DBF file. You can also omit the -o stamnett.sqlite option to have the SQL printed directly to stdout.

Contact

Ole Martin Bjorndalen - ombdalen@gmail.com

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dbfread-0.1.0.tar.gz (12.8 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page