1

I have a series of dBase files and I'd like to convert them to text files Anyone can help me? I've tried table to table, doesn't seem to work.

The idea is to get ASCII files so I can do further analysis with R script. My current way is to run exportXYv_stats tool, I need a way to avoid manually adding fields as parameter.

  • You could use cursors and the csv module. – Paul Oct 28 '15 at 19:11
4

For this task you don't need arcpy, only dbfpy library. Here is the code found on github:

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"
  • Hey thanks! I've seen this from git-hub already, when i tried to use it, i had error saying-- ImportError: No module named dbfpy. Do you know if i need to import something else other than csv? – MaxG Oct 28 '15 at 19:28
  • you need to install dbfpy: pypi.python.org/pypi/dbfpy/2.2.0 Download exe and run it if you are on Windows: sourceforge.net/projects/dbfpy/files – Matej Oct 28 '15 at 19:30
  • Great! Though I'm working on government PC, trying to avoid install new programs . I will look for another way to solve this, if not I will try this one. Thanks a lot though! – MaxG Oct 28 '15 at 19:41
  • I mean, this is just a python library.. Ideally, you would want to create a virtualenv and install libraries there. That way, it would be completely isolated from the operating system. The quick and dirty way is to install it globally by running exe. – Matej Oct 28 '15 at 19:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.