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
1 Answer 1
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
-
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– MaxG2015年10月28日 19:28:53 +00:00Commented 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/filesMatej– Matej2015年10月28日 19:30:53 +00:00Commented 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!MaxG– MaxG2015年10月28日 19:41:24 +00:00Commented 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.Matej– Matej2015年10月28日 19:50:22 +00:00Commented Oct 28, 2015 at 19:50
lang-py
csv
module.