Skip to content

Instantly share code, notes, and snippets.

@celisflen-bers
Forked from bertspaan/README.md
Last active March 11, 2022 11:45
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save celisflen-bers/fe827aa724997b0487a084d225054e2c to your computer and use it in GitHub Desktop.
Save celisflen-bers/fe827aa724997b0487a084d225054e2c 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")
@mahalel
Copy link

mahalel commented Jul 1, 2020

You saved my bacon with this, Thank you!

@diogoamore
Copy link

diogoamore commented Jul 23, 2020

Fellas, I ve been having issues with slots from fields.py as I try to run the code:

import fields
File "C:\Users\Amore\AppData\Local\Programs\Python\Python36-32\Lib\site-packages\dbfpy\fields.py", line 44, in <module>
class DbfFieldDef(object):
ValueError: 'length' in __slots__ conflicts with class variable

Any thoughts on how to solve it?

Cheers

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