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.

Hornbydd
44.9k5 gold badges43 silver badges84 bronze badges
asked Oct 28, 2015 at 19:04
1
  • You could use cursors and the csv module. Commented Oct 28, 2015 at 19:11

1 Answer 1

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"
answered Oct 28, 2015 at 19:20
4
  • 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? Commented Oct 28, 2015 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 Commented Oct 28, 2015 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! Commented Oct 28, 2015 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. Commented Oct 28, 2015 at 19:50

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.