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