- First download
dbfpy
: http://sourceforge.net/projects/dbfpy/files/latest/download?source=files - Then install:
sudo python setup.py install
To convert DBF file to CSV:
./dbf2csv database.dbf
dbfpy
: http://sourceforge.net/projects/dbfpy/files/latest/download?source=filessudo python setup.py install
To convert DBF file to CSV:
./dbf2csv database.dbf
#!/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" |
how to install dbfpy module in virtual env.?
Thanks!
@rpsingh21 just pip install dbfpy
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'
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"
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/)
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
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?
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?
Nice dude ;)
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")
my dbf contain UTF8 and/or chinese words. currently the data became monster words ( e.g. ∂◊¬◊´H¶´ ). how can i fix it ?
I apply this for one file and I got