Skip to content

Instantly share code, notes, and snippets.

@bertspaan
Created January 2, 2014 15:28
Show Gist options
  • Select an option

  • Save bertspaan/8220892 to your computer and use it in GitHub Desktop.

Select an option

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"
@Stavrakoss

Copy link
Copy Markdown

Thanks mate, that's really handy.

@0t3dWCE

0t3dWCE commented Jul 27, 2015

Copy link
Copy Markdown

Just that I need. Spasibo, tovarish.

@jhli973

jhli973 commented Feb 24, 2016

Copy link
Copy Markdown

cool, thanks

@rilarios

rilarios commented Mar 3, 2016

Copy link
Copy Markdown

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

@riordan

riordan commented May 1, 2016

Copy link
Copy Markdown

Of course you wrote this, @bertspaan!

@santu1987

Copy link
Copy Markdown

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

mruepp commented May 19, 2016

Copy link
Copy Markdown

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

@bolatov

bolatov commented May 26, 2016

Copy link
Copy Markdown

@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
Copy Markdown

@Felipe3000W

Copy link
Copy Markdown

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

gtdca98 commented Jan 6, 2017

Copy link
Copy Markdown

Thanks. Simple and effective

@rolando-urrea

Copy link
Copy Markdown

Thanks mate! It was very helpful!

@Cyberguille

Copy link
Copy Markdown

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: '**********'

@martinmanzo

Copy link
Copy Markdown

Thanks!

@rpsingh21 just pip install dbfpy

@tvequaud

Copy link
Copy Markdown

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

agustinramos commented Oct 27, 2017

Copy link
Copy Markdown

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

AlJohri commented Oct 29, 2017

Copy link
Copy Markdown

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

mateice commented Nov 30, 2017

Copy link
Copy Markdown

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

cg342 commented Mar 6, 2018

Copy link
Copy Markdown

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
Copy Markdown

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

aluzed commented Oct 18, 2018

Copy link
Copy Markdown

Nice dude ;)

@avissian

avissian commented Mar 20, 2019

Copy link
Copy Markdown

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
Copy Markdown

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

@traut21

traut21 commented Aug 8, 2024

Copy link
Copy Markdown

I use Python 3.12.4 on MacOS 14.5
setup.py here complains:
` self.initialize_options()
File "build/bdist.macosx-14.0-arm64/egg/dbfpy/dbfnew.py", line 155
print "*** created tst.dbf: ***"
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)?

File "build/bdist.macosx-14.0-arm64/egg/dbfpy/dbf.py", line 280
print repr(_rec)
^^^^^^^^^^^^^^^^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)?
`

@0t3dWCE

0t3dWCE commented Aug 8, 2024

Copy link
Copy Markdown

@traut21 next time first thing you should do - paste output to translater.
Missing parentheses in call to 'print'. Did you mean print(...)? means you use old fashion print "*** created tst.dbf: ***" without parentheses, and if you'll seat near me, i'll use big boring book about algorithms to kick your head (this said with friendly voice:)
Happy learning =)

@traut21

traut21 commented Aug 8, 2024

Copy link
Copy Markdown

Thanks for your "friendly voice". So I will have to learn python before I can use this github script, right? No hint whether my Python version is too new, too old or is not properly supported on MacOS.

These parts of the python software were not written by myself, but were created by the setup.py. So I see, it's my fault that the setup does not create what I need. Instead of fixing the first bug messages and then wait for the next problems I'll have a look for another dbf converter first.

@martinmanzo

Copy link
Copy Markdown

@traut21 the script is quite old and was originally written for Python 2.7.

To make it compatible with Python 3, you'll need to call the print statements as a function. Simply add parentheses around the print statements, and that should do the trick!

Here's the updated version of the script:

#!/usr/bin/python

import csv
from dbfpy import dbf
import os
import sys

filename = sys.argv[1]
if filename.endswith('.dbf'):
    print(f"Converting {filename} to csv")
    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")

@traut21

traut21 commented Aug 8, 2024

Copy link
Copy Markdown

thanks, now I know that this script had been written for P 2.7

Your script tells me
print repr(_rec) ^ SyntaxError: invalid syntax

@martinmanzo

Copy link
Copy Markdown

Last release of dbfpy was in 2015, most certainly it wasn't updated to py3.

If this is only a one time use just install python 2.7.

@traut21

traut21 commented Aug 8, 2024

Copy link
Copy Markdown

Thanks, I'll give that a try. Yes, it's a one time use where the dbf has too many lines for LibreOffice

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