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"
@bertspaan
Copy link
Author

@Stavrakoss
Copy link

Thanks mate, that's really handy.

@0t3dWCE
Copy link

0t3dWCE commented Jul 27, 2015

Just that I need. Spasibo, tovarish.

@jhli973
Copy link

jhli973 commented Feb 24, 2016

cool, thanks

@rilarios
Copy link

rilarios commented Mar 3, 2016

This is great.. thank you very much!! Gracias!

@riordan
Copy link

riordan commented May 1, 2016

Of course you wrote this, @bertspaan!

@santu1987
Copy link

Good afternoon, thanks for your code, but i have a doubt,
I have a dbf file of 2gb, your program export this a csv file very good, but i need that this csv has comlums wth character delimiter ";"...

@mruepp
Copy link

mruepp commented May 19, 2016

Thank you for the script. Unfortunately, it handles ÄÖÜ and stuff not correct, must be a codepage problem. I guess its in dbfpy.

@bolatov
Copy link

bolatov commented May 26, 2016

@bertspaan thank you very much!

@mruepp I think the problem is in the encoding. Try to convert your dbf to csv and then specify the encoding when opening the csv file.

@celisflen-bers
Copy link

@Felipe3000W
Copy link

If you want to change the delimiter try this at line 14

out_csv = csv.writer(csvfile,delimiter='|')
where "|" would be the delimiter... you can choose ";" "@" ,etc

@gtdca98
Copy link

gtdca98 commented Jan 6, 2017

Thanks. Simple and effective

@rolando-urrea
Copy link

Thanks mate! It was very helpful!

@Cyberguille
Copy link

I apply this for one file and I got

Traceback (most recent call last):
  File "dbf2csv.py", line 19, in <module>
    for rec in in_db:
  File "/usr/local/lib/python2.7/dist-packages/dbfpy/dbf.py", line 262, in __getitem__
    return self.RecordClass.fromStream(self, self._fixIndex(index))
  File "/usr/local/lib/python2.7/dist-packages/dbfpy/record.py", line 121, in fromStream
    return cls.fromString(dbf, cls.rawFromStream(dbf, index), index)
  File "/usr/local/lib/python2.7/dist-packages/dbfpy/record.py", line 140, in fromString
    [_fd.decodeFromRecord(string) for _fd in dbf.header.fields])
  File "/usr/local/lib/python2.7/dist-packages/dbfpy/fields.py", line 178, in decodeFromRecord
    return self.decodeValue(self.rawFromRecord(record))
  File "/usr/local/lib/python2.7/dist-packages/dbfpy/fields.py", line 249, in decodeValue
    return int(value)
ValueError: invalid literal for int() with base 10: '**********'

@rpsingh21
Copy link

how to install dbfpy module in virtual env.?

@martinmanzo
Copy link

Thanks!

@rpsingh21 just pip install dbfpy

@tvequaud
Copy link

Don't work with my DBF file.

Tried this :

import sys
from dbfpy import dbf

db = dbf.Dbf("/home/thomas/repert1709.dbf")
print db
print

And got :

Traceback (most recent call last):
  File "./converter.py", line 7, in <module>
    db = dbf.Dbf("/home/thomas/repert1709.dbf")
  File "/home/thomas/.local/lib/python2.7/site-packages/dbfpy/dbf.py", line 138, in __init__
    self.header = self.HeaderClass.fromStream(self.stream)
  File "/home/thomas/.local/lib/python2.7/site-packages/dbfpy/header.py", line 128, in fromStream
    _fld = fields.lookupFor(_data[11]).fromString(_data, _pos)
  File "/home/thomas/.local/lib/python2.7/site-packages/dbfpy/fields.py", line 473, in lookupFor
    return _fieldsRegistry[typeCode]
KeyError: 'B'

@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