Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 639
Tom Avatar asked Sep 01 '08 06:09

Tom


People also ask

How do I read a DBF file in Python?

If you know what it should be, you can either open it with that codepage (temporarily) with: table = dbf. Table('file. DBF', codepage='...')

How do I open a FoxPro DBF file?

Open a Microsoft Access database. From the File menu, choose Get External Data then Import. In the Import dialog box, select ODBC Databases in the Files of type list. In the SQL Data Sources dialog box, select the Visual FoxPro data source that connects to the FoxPro data you want to query and click OK.


2 Answers

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.

like image 185
Anders Sandvig Avatar answered Sep 22 '22 11:09

Anders Sandvig


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.

like image 24
Steve Koch Avatar answered Sep 24 '22 11:09

Steve Koch