Skip to content

Instantly share code, notes, and snippets.

@danabauer
Last active December 17, 2015 11:39
Show Gist options
  • Save danabauer/5603439 to your computer and use it in GitHub Desktop.
Save danabauer/5603439 to your computer and use it in GitHub Desktop.
Quick script to convert dbf for csv. Tested with large 2010 census block data
"""
---------------------------------------------------------------------------
dbf_to_csv.py
Dana Bauer
Feb 1, 2012
---------------------------------------------------------------------------
"""
import csv
from dbfpy import dbf
try:
dbfFile = dbf.Dbf(open(r'c:\projects\MN\MN_block_name.dbf','r'))
csvFile = csv.writer(open(r'c:\projects\MN\MNblocks.csv', 'wb'))
headers = range(len(dbfFile.fieldNames))
allRows = []
for row in dbfFile:
rows = []
for num in headers:
rows.append(row[num])
allRows.append(rows)
csvFile.writerow(dbfFile.fieldNames)
for row in allRows:
print row
csvFile.writerow(row)
except Exception,e:
print e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment