Skip to content

Instantly share code, notes, and snippets.

@bertspaan
Created January 2, 2014 15:28
Show Gist options
  • Star 82 You must be signed in to star a gist
  • Fork 13 You must be signed in to fork a gist
  • Save bertspaan/8220892 to your computer and use it in GitHub Desktop.
Save bertspaan/8220892 to your computer and use it in GitHub Desktop.
Python script to convert DBF database file to CSV
#!/usr/bin/python
import csv
from dbfpy import dbf
import os
import sys
filename = sys.argv[1]
if filename.endswith('.dbf'):
print "Converting %s to csv" % filename
csv_fn = filename[:-4]+ ".csv"
with open(csv_fn,'wb') as csvfile:
in_db = dbf.Dbf(filename)
out_csv = csv.writer(csvfile)
names = []
for field in in_db.header.fields:
names.append(field.name)
out_csv.writerow(names)
for rec in in_db:
out_csv.writerow(rec.fieldData)
in_db.close()
print "Done..."
else:
print "Filename does not end with .dbf"
@agustinramos
Copy link

agustinramos commented Oct 27, 2017

If you need to filter columns you can use this script
Try this:

#!/usr/bin/python

import csv
from dbfpy import dbf
import os
import sys
import json
from argparse import ArgumentParser

description = 'Arguments of DBF to CSV converter'
parser = ArgumentParser(description=description)
parser.add_argument('-f', '--file', dest='file', required='true',
                    help="Examples: -i file.dbf")

parser.add_argument('-c', '--colunms', dest='colunms',
                    type=str, nargs='*',
                    help="Examples: -c item1 item2 item3")

opts = parser.parse_args()

if opts.file.endswith('.dbf') or opts.file.endswith('.DBF'):
    print "Converting %s to csv" % opts.file
    csv_fn = opts.file[:-4]+ ".csv"
    with open(csv_fn,'wb') as csvfile:
        in_db = dbf.Dbf(opts.file)
        out_csv = csv.writer(csvfile)
        names = []
        colunmsKeySave = {}
        for keyF, field in enumerate(in_db.header.fields):            
            if opts.colunms:
                for ig in opts.colunms:
                    if ig == field.name:
                        names.append(field.name)
                        colunmsKeySave[keyF] = field.name
            else:
                names.append(field.name)
        out_csv.writerow(names)

        for rec in in_db:
            if opts.colunms:
                values = []
                for keyf, field in enumerate(rec.fieldData):
                    if keyf in colunmsKeySave:
                        values.append(field)
                out_csv.writerow(values)
            else:
                out_csv.writerow(rec.fieldData)

        in_db.close()
        print "Done..."
else:
  print "Filename does not end with .dbf"

@AlJohri
Copy link

AlJohri commented Oct 29, 2017

This library worked for me: https://github.com/akadan47/dbf2csv
For Python 3: akadan47/dbf2csv#1

It uses the underlying dbfread library (https://github.com/olemb/dbfread) instead of dbfpy (http://dbfpy.sourceforge.net/)

@mateice
Copy link

mateice commented Nov 30, 2017

If you need to filter columns you can use this script
Try this:

Plese tell what to write in the line of

description = 'Arguments of DBF to CSV converter'
parser = ArgumentParser(description=description)
parser.add_argument('-f', '--file', dest='file', required='true',
help="Examples: -i file.dbf")

parser.add_argument('-c', '--colunms', dest='colunms',
type=str, nargs='*',
help="Examples: -c item1 item2 item3")

I have DBF file with 9500 lines and and A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,,R,S,T,U,W,X,Y,Z,AA,AB.... AQ columnes.

And i have to convert the last 130 lines with all columnes
Any idea.
Thank you

@cg342
Copy link

cg342 commented Mar 6, 2018

when I tried this code:

from dbfpy import dbf
path = "a/path/to/dbf/file/"
in_db = dbf.Dbf(path)

immediately I got this error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/dbfpy/dbf.py", line 128, in __init__
    self.stream = file(f, ("r+b", "rb")[bool(readOnly)])
IOError: [Errno 13] Permission denied: 'a/path/to/dbf/file.DBF'

Somehow it works with some of my .DBF files, but not all... Is there something wrong with my files? or limitation with dbf.Dbf() function?

@shreyamsh
Copy link

I am running this on Python 3 and it throws the error on dbf. NameError: name 'Dbf' is not defined.
Any suggestions on how to proceed with this?

@aluzed
Copy link

aluzed commented Oct 18, 2018

Nice dude ;)

@avissian
Copy link

avissian commented Mar 20, 2019

when I tried this code:

from dbfpy import dbf
path = "a/path/to/dbf/file/"
in_db = dbf.Dbf(path)

immediately I got this error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/dbfpy/dbf.py", line 128, in __init__
    self.stream = file(f, ("r+b", "rb")[bool(readOnly)])
IOError: [Errno 13] Permission denied: 'a/path/to/dbf/file.DBF'

Somehow it works with some of my .DBF files, but not all... Is there something wrong with my files? or limitation with dbf.Dbf() function?

Scan the directory for all DBF files. File extension case-insensitive. And commented code that writes the header

#!/usr/bin/python

import csv
from dbfpy import dbf
import os
import sys

csv_fn = "all.csv"
with open(csv_fn,'wb') as csvfile:
  for file_name in os.listdir('.'):
    if file_name.lower().endswith('.dbf'):
      print ("Converting %s to csv" % file_name)
      in_db = dbf.Dbf(file_name)
      out_csv = csv.writer(csvfile)

      #names = [] # no header
      #for field in in_db.header.fields:
      #names.append(field.name)

      for rec in in_db:
          out_csv.writerow(rec.fieldData)
      in_db.close()
      print ("Done...")
    else:
      print ("Filename does not end with .dbf")

@Calvin2274
Copy link

my dbf contain UTF8 and/or chinese words. currently the data became monster words ( e.g. ∂◊¬◊´H¶´ ). how can i fix it ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment