Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

bertspaan commented Jan 2, 2014

@Stavrakoss

This comment has been minimized.

Copy link

Stavrakoss commented Mar 26, 2015

Thanks mate, that's really handy.

@0t3dWCE

This comment has been minimized.

Copy link

0t3dWCE commented Jul 27, 2015

Just that I need. Spasibo, tovarish.

@jhli973

This comment has been minimized.

Copy link

jhli973 commented Feb 24, 2016

cool, thanks

@rilarios

This comment has been minimized.

Copy link

rilarios commented Mar 3, 2016

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

@riordan

This comment has been minimized.

Copy link

riordan commented May 1, 2016

Of course you wrote this, @bertspaan!

@santu1987

This comment has been minimized.

Copy link

santu1987 commented May 10, 2016

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

This comment has been minimized.

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

This comment has been minimized.

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

This comment has been minimized.

@Felipe3000W

This comment has been minimized.

Copy link

Felipe3000W commented Nov 17, 2016

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

This comment has been minimized.

Copy link

gtdca98 commented Jan 6, 2017

Thanks. Simple and effective

@rolando-urrea

This comment has been minimized.

Copy link

rolando-urrea commented Jun 2, 2017

Thanks mate! It was very helpful!

@Cyberguille

This comment has been minimized.

Copy link

Cyberguille commented Jul 13, 2017

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

This comment has been minimized.

Copy link

rpsingh21 commented Aug 4, 2017

how to install dbfpy module in virtual env.?

@martinmanzo

This comment has been minimized.

Copy link

martinmanzo commented Oct 9, 2017

Thanks!

@rpsingh21 just pip install dbfpy

@tvequaud

This comment has been minimized.

Copy link

tvequaud commented Oct 20, 2017

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

This comment has been minimized.

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

This comment has been minimized.

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

This comment has been minimized.

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

This comment has been minimized.

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

This comment has been minimized.

Copy link

shreyamsh commented Mar 13, 2018

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

This comment has been minimized.

Copy link

aluzed commented Oct 18, 2018

Nice dude ;)

@avissian

This comment has been minimized.

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")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.