What's the easiest way to read a FoxPro DBF file from Python?
Asked Answered
B

6

25

I've got a bunch of FoxPro (VFP9) DBF files on my Ubuntu system, is there a library to open these in Python? I only need to read them, and would preferably have access to the memo fields too.

Update: Thanks @cnu, I used Yusdi Santoso's dbf.py and it works nicely. One gotcha: The memo file name extension must be lower case, i.e. .fpt, not .FPT which was how the filename came over from Windows.

Brunson answered 1/9, 2008 at 6:45 Comment(0)
A
8

You can try this recipe on Active State.

There is also a DBFReader module which you can try.

For support for memo fields.

Allocution answered 1/9, 2008 at 7:2 Comment(3)
The DBFReader module is now a dead link.Illaudable
In memo fields program its give the number not exact data how to retrieve the data based on that index number where that stored .Marji
Since Sep 2008 (time of the answer), there are many more options (Nov 2015), for example: pypi.python.org/pypi/dbf, pypi.python.org/pypi/dbfread, sourceforge.net/projects/dbfpy. I personally chose dbfread because it is well documented.Maniac
D
17

I prefer dbfpy. It supports both reading and writing of .DBF files and can cope with most variations of the format. It's the only implementation I have found that could both read and write the legacy DBF files of some older systems I have worked with.

Dric answered 1/9, 2008 at 13:12 Comment(0)
P
13

I was able to read a DBF file (with associated BAK, CDX, FBT, TBK files**) using the dbf package from PyPI http://pypi.python.org/pypi/dbf . I am new to python and know nothing about DBF files, but it worked easily to read a DBF file from my girlfriend's business (created with a music store POS application called AIMsi).

After installing the dbf package (I used aptitude and installed dbf version 0.88 I think), the following python code worked:

from dbf import *
test = Table("testfile.dbf")
for record in test:
    print record
    x = raw_input("")  # to pause between showing records

That's all I know for now, but hopefully it's a useful start for someone else who finds this question!

April 21, 2012 SJK Edit: Per Ethan Furman's comment, I should point out that I actually don't know which of the data files were necessary, besides the DBF file. The first time I ran the script, with only the DBF available, it complained of a missing support file. So, I just copied over the BAK, CDX, FPT (not FBT as I said before edit), TBK files and then it worked.

Platter answered 20/4, 2012 at 23:33 Comment(5)
This package doesn't yet support IDX/CDX files, although I hope to have that in place this year.Caddell
Are you sure it was FBT and not FPT? FPT files are the memo files. CDX is the index file although @Ethan says these aren't supported so that was unlikely to be the missing file. Along with the DBF these are the only 3 files you should need to read a DBF/Table.Illaudable
This package is capable but documentation is not newbie friendly, dbfread provides more examples.Maniac
You might need to add test.open() before the for loop for this to workTercet
@Steve are you sure it works with FPT files too? Or anyone else? I am trying to read FPT files but I am new to DBF and do not know what to do now. HELP pleaseMeemeece
B
9

If you're still checking this, I have a GPL FoxPro-to-PostgreSQL converter at https://github.com/kstrauser/pgdbf . We use it to routinely copy our tables into PostgreSQL for fast reporting.

Billat answered 17/9, 2008 at 18:59 Comment(1)
I coincidentally saw this while checking for something else. Now a postgreSQL lover, coming from VFP and MS SQL, this is much appreciated. Thanks!Skepticism
A
8

You can try this recipe on Active State.

There is also a DBFReader module which you can try.

For support for memo fields.

Allocution answered 1/9, 2008 at 7:2 Comment(3)
The DBFReader module is now a dead link.Illaudable
In memo fields program its give the number not exact data how to retrieve the data based on that index number where that stored .Marji
Since Sep 2008 (time of the answer), there are many more options (Nov 2015), for example: pypi.python.org/pypi/dbf, pypi.python.org/pypi/dbfread, sourceforge.net/projects/dbfpy. I personally chose dbfread because it is well documented.Maniac
P
5

Check out http://groups.google.com/group/python-dbase

It currently supports dBase III and Visual Foxpro 6.0 db files... not sure if the file layout change in VFP 9 or not...

Prudence answered 23/2, 2009 at 17:8 Comment(0)
R
5

It's 2016 now and I had to fiddle with the dbf package to get it to work. Here is a python3 version to just export a dbf file to a csv

import dbf

d=dbf.Table('mydbf.dbf')
d.open()
dbf.export(d, filename='mydf_exported.csv', format='csv', header=True)

I had some unicode error at first, but got around that by turning off memos.

import dbf

d=dbf.Table('mydbf.dbf', ignore_memos=True)
d.open()
dbf.export(d, filename='mydf_exported.csv', format='csv', header=True)
Rheotaxis answered 2/8, 2016 at 21:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.