- 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" |
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 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?