You are here: > ESRI Forums > arcgis desktop discussion forums > Thread Replies

ArcGIS Desktop Discussion Forums

ArcGIS Desktop - Geoprocessing Scripting (Python, JavaScript, VB) forum

Converting DBF to CSV   Steve Maskol Jan 19, 2007
Re: Converting DBF to CSV   R.D. Harles Jan 19, 2007
Re: Converting DBF to CSV   David Wynne Jan 19, 2007
Re: Converting DBF to CSV   Steve Maskol Jan 22, 2007
Re: Converting DBF to CSV   Philippe Le Grand Jan 22, 2007
Re: Converting DBF to CSV   Geoff Klise Jan 22, 2007
Re: Converting DBF to CSV   Chris Snyder Jan 22, 2007
Re: Converting DBF to CSV   Geoff Klise Jan 22, 2007
Re: Converting DBF to CSV   R.D. Harles Jan 22, 2007
Re: Converting DBF to CSV   Geoff Klise Jan 22, 2007
Re: Converting DBF to CSV   R.D. Harles Jan 22, 2007
Re: Converting DBF to CSV   Steve Maskol Jan 23, 2007
Re: Converting DBF to CSV   Geoff Klise Jan 23, 2007
Re: Converting DBF to CSV   R.D. Harles Jan 23, 2007
Re: Converting DBF to CSV   Darren McCormick Sep 28, 2007
Re: Converting DBF to CSV   Geoff Klise Sep 28, 2007
Re: Converting DBF to CSV   Darren McCormick Sep 28, 2007
Re: Converting DBF to CSV   R.D. Harles Sep 28, 2007
Re: Converting DBF to CSV   Darren McCormick Oct 01, 2007
Re: Converting DBF to CSV   R.D. Harles Oct 01, 2007
Re: Converting DBF to CSV   Darren McCormick Oct 19, 2007
Re: Converting DBF to CSV   Gavin McGhie Aug 07, 2009
Re: Converting DBF to CSV   Gavin McGhie Aug 07, 2009
Re: Converting DBF to CSV   R.D. Harles Aug 07, 2009
Re: Converting DBF to CSV   Gavin McGhie Aug 07, 2009
Re: Converting DBF to CSV   Mattias Wallin Aug 11, 2009
Re: Converting DBF to CSV   Peter MacKenzie Aug 26, 2009
Re: Converting DBF to CSV   Geoff Klise Aug 26, 2009
Re: Converting DBF to CSV   Mattias Wallin Aug 26, 2009
Re: Converting DBF to CSV   Peter MacKenzie Aug 27, 2009
Re: Converting DBF to CSV   Ken Carrier Nov 20, 2009
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Converting DBF to CSV 
Author Steve Maskol 
Date Jan 19, 2007 
Message I have been using Model Builder and Python scripting to perform several operations with our parcel data. I have the results output to DBF tables. Is there a way in Python to convert the DBF tables to CSV? Thanks for the help. 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Converting DBF to CSV 
Author R.D. Harles 
Date Jan 19, 2007 
Message If there is an easy way, I'd love to know. A while back I looked into this. This was the only lead I got and it looked quite complicated and I never pursued it further. I ended up converting the files I had the old fashioned way.....opening them up in ArcView 3.3 and exporting them back out as delimited text.

http://groups.google.com/group/comp.lang.python/browse_thread/thread/c381c0349a2fd7b2/44cd62dc9c2840da?lnk=gst&q=convert+dbf&rnum=1#44cd62dc9c2840da 
  -----------------
R.D. Harles
ADCi
www.adci.com 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Converting DBF to CSV 
Author David Wynne 
Date Jan 19, 2007 
Message Hi Steve,
There are a few directions that can be taken, including the csv module in Python. However, if you're just looking for a quick and easy dump of data into csv format though, a pretty simple way is to call Excel in python to handle the task. 
 
import win32com.client
excel = win32com.client.Dispatch('Excel.Application')

inDBF = r"c:\temp\somedbf.dbf"
outCSV = r"c:\temp\mycsv.csv"

workbook = excel.Workbooks.Open(inDBF)
# 24 represents xlCSVMSDOS
workbook.SaveAs(outCSV,FileFormat=24)

excel.Quit()
 
  Dave Wynne
ESRI 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Converting DBF to CSV 
Author Steve Maskol 
Date Jan 22, 2007 
Message David,

Thanks for the code. I tried it out but I am getting errors. I have three files I am trying to convert to CSV. When I run the script from the PythonWin in converts the first file called Splits_VW to CSV and then the following message box opens.

Do you want to save the changes you made to Splits_VW.csv?

If I click Yes or No the script ends with errors. Here is the error that is returned.


Traceback (most recent call last):
File "c:\python21\pythonwin\pywin\framework\scriptutils.py", line 310, in RunScript
exec codeObject in __main__.__dict__
File "H:\python\Parcels\DBFtoCSV.py", line 28, in ?
workbook = excel.Workbooks.Open(inRecombinesDBF)
File "c:\python21\win32com\client\dynamic.py", line 454, in __getattr__
raise pythoncom.com_error, details
com_error: (-2147418111, 'Call was rejected by callee.', None, None)
>>>


I have included my code. It is essentially the code you posted but I am trying to copy three files.

Any ideas why I am getting the errors? Thanks for the help.
 
 
# Import system modules
import sys, string, os, win32com.client
import win32com.client

# Create the Geoprocessor object
excel = win32com.client.Dispatch('Excel.Application')

# Convert DBF files to CSV
inSplitsDBF = r"c:\GISTemp\Splits_VW.dbf"
inRecombinesDBF = r"c:\GISTemp\Recombine_VW.dbf"
inUnionDBF = r"c:\GISTemp\U_NewOld_VW.dbf"
outSplitsCSV = r"c:\GISTemp\Splits_VW.csv"
outRecombinesCSV = r"c:\GISTemp\Recombine_VW.csv"
outUnionCSV = r"c:\GISTemp\U_NewOld_VW.csv"

# 24 represents xlCSVMSDOS
workbook = excel.Workbooks.Open(inSplitsDBF)
workbook.SaveAs(outSplitsCSV,FileFormat=24)
excel.Quit()

workbook = excel.Workbooks.Open(inRecombinesDBF)
workbook.SaveAs(outRecombinesCSV,FileFormat=24)
excel.Quit()

workbook = excel.Workbooks.Open(inUnionDBF)
workbook.SaveAs(outUnionCSV,FileFormat=24)
excel.Quit()
 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Converting DBF to CSV 
Author Philippe Le Grand 
Date Jan 22, 2007 
Message Steve,

when you call excel.Quit(), the external Excel application closes and the excel variable no longer points to an active Excel instance.

You could try to re-instantiate the Excel application (via a new call to win32com.client.dispatch), or you could try to remove the intermediate excel.Quit() calls.

Good Luck,

Philippe 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Converting DBF to CSV 
Author Geoff Klise 
Date Jan 22, 2007 
Message David, great timing with the code for this thread! One question, how do you use both excel and gp in the win32com.client.Dispatch? my python coding is limited to the geoprocessor object, and when I try to loop through dbf files using ListTables, I get a random assortment of errors. Is there a way to loop through the dbf files in python without calling the geoprocessor obeject? the gp. looping is so easy, but when I look for python code, I can't find something simple.

Thanks, 
  Geoff Klise
Sandia National Laboratories 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Converting DBF to CSV 
Author Chris Snyder 
Date Jan 22, 2007 
Message How about:
 
 
import glob
dbfList = glob.glob(C:\\temp\\*.dbf)
for dbfFile in dbfList:
    do something
print "All done!"
 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Converting DBF to CSV 
Author Geoff Klise 
Date Jan 22, 2007 
Message I tried something similar using glob, then tried your variation, but I get the same error message saying it can't find the file that it found!!!!

Traceback (most recent call last):
File "C:\Documents and Settings\gklise\Desktop\dbf_to_csv.py", line 12, in ?
workbook = excel.Workbooks.Open(dbfList)
File ">", line 2, in Open
com_error: (-2147352567, 'Exception occurred.', (0, 'Microsoft Office Excel', "'['C:\\\\junk\\\\01-17-1980.dbf', 'C:\\\\junk\\\\01-18-1980.dbf'].xls' could not be found. Check the spelling of the file name, and verify that the file location is correct.\n\nIf you are trying to open the file from your list of most recently used files on the File menu, make sure that the file has not been renamed, moved, or deleted.", 'C:\\Program Files\\Microsoft Office\\OFFICE11\\1033\\xlmain11.chm', 0, -2146827284), None) 
 
import win32com.client, sys, os, string, glob
excel = win32com.client.Dispatch('Excel.Application')
import traceback

inDBF = sys.argv[1]
outputFolder = sys.argv[2]

try:   
    dbfList = glob.glob("C:\\junk\\*.dbf") 
    outCSV = outputFolder + "\\" + inDBF
    for dbfFile in dbfList:
        workbook = excel.Workbooks.Open(dbfList)
        # 24 represents xlCSVMSDOS
        workbook.SaveAs(outCSV,FileFormat=24)
        
    excel.Quit()
    
except:
    traceback.print_exc()
 
  Geoff Klise
Sandia National Laboratories 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Converting DBF to CSV 
Author R.D. Harles 
Date Jan 22, 2007 
Message ...looks like you just need to change dbfList to dbfFile in this line:

workbook = excel.Workbooks.Open(dbfFile) 
  -----------------
R.D. Harles
ADCi
www.adci.com 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Converting DBF to CSV 
Author Geoff Klise 
Date Jan 22, 2007 
Message Thanks, I did try that and got a different error saying the file could not be accessed. Maybe I should just use one of those shareware programs to do this.

The original code that was posted worked fine for one file and I noticed that there was an "r" in the path name. Not sure why. 
  Geoff Klise
Sandia National Laboratories 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Converting DBF to CSV 
Author R.D. Harles 
Date Jan 22, 2007 
Message In case anyone cares, the following code worked for me.

I added a "close" after each save and that stopped all the pop-ups and hanging of EXCEL.EXE.

It simply saves the csv files in the same directory as the dbf's. 
 
import win32com.client, sys, os, string, glob

gp = win32com.client.Dispatch("esriGeoprocessing.GpDispatch.1")
excel = win32com.client.Dispatch('Excel.Application')

gp.workspace = os.getcwd()

try:   
    dbfList = glob.glob("C:\\junk\\dbf2csv\\*.dbf") 
    for dbfFile in dbfList:
        # Strip the ".dbf" and add ".csv"
        outCSV = dbfFile[:-4]+".csv"
        # Open each dbf file
        workbook = excel.Workbooks.Open(dbfFile)
        # 24 represents xlCSVMSDOS
        workbook.SaveAs(outCSV, FileFormat=24)
        # Close workbook after each save
        workbook.Close(False)

    # Quit Excel    
    excel.Quit()        
    
    
except:
    print "\n *** Error *** \n"
    workbook.Close(False)
    excel.Quit()
 
  -----------------
R.D. Harles
ADCi
www.adci.com 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Converting DBF to CSV 
Author Steve Maskol 
Date Jan 23, 2007 
Message Thanks for the help with my Python question.

I have one follow up question about using "close". In your code you use workbook.Close(False). In example code I found on a Python code website workbook.Close(SaveChanges=0) is used. I tried it this way and it appears to work okay. The pop-ups stopped and EXCEL is not hanging.

Is workbook.Close(False) and workbook.Close(SaveChanges=0) pretty much the same thing? Are they both working the same way? Is one better/safer to use over the other? 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Converting DBF to CSV 
Author Geoff Klise 
Date Jan 23, 2007 
Message I got it to work after using workbook.Close(SaveChanges=0). With Close(False) it would create one file, and bomb. When I tried to delete the file, it said it was still open, even though Excel was closed. I had to reopen it then close it to delete the file.

Thanks for all your help, it's not everyday you look at the postings and someone has the exact same problem you're having. I have yet to call tech suppport.... :) 
  Geoff Klise
Sandia National Laboratories 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Converting DBF to CSV 
Author R.D. Harles 
Date Jan 23, 2007 
Message It is my understanding that they are equal (although I'm not positive). I saw quite a few different ways to tell Excel not to pop-up the "Do you want to save changes to xxx?" I tried both ways in the code I posted above and they did exact same thing.
 
  -----------------
R.D. Harles
ADCi
www.adci.com 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Converting DBF to CSV 
Author Darren McCormick 
Date Sep 28, 2007 
Message Hi:

I am having trouble getting Workbook.Close(False) to work with python 2.1. Should it work with this version?

Here is the code I am using:


import win32com.client, sys, os, string
excel = win32com.client.Dispatch('Excel.Application')

inDBF = r"E:\my_data\test.dbf"
outCSV = r"E:\my_data\test.csv"

workbook = excel.Workbooks.Open(inDBF)
# 24 represents xlCSVMSDOS
workbook.SaveAs(outCSV,FileFormat=24)

# Close workbook after each save
workbook.close(false)
# Quit Excel
excel.Quit()


A popoup asks if I want to save the changes as if workbook.close(false) was not event there. An error message is reported that says:

NameError: name 'false' is not defined

The test.csv is created ok.


Thanks - any help will be appreciated!

-Darren
 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Converting DBF to CSV 
Author Geoff Klise 
Date Sep 28, 2007 
Message Darren, My final code for this section is:

workbook.Close(SaveChanges=0)
excel.Quit()

I haven't used it since my messages were posted and I think I did have probelms with using "false". Good Luck!

 
  Geoff Klise
Sandia National Laboratories 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Converting DBF to CSV 
Author Darren McCormick 
Date Sep 28, 2007 
Message Hi Geoff:

Thanks for the quick reply!

I should have mentioned in my first post that I also tried workbook.Close(SaveChanges=0). With this approach I don't get an error message, but the Excel popup is generated to ask if I want to save the csv file.

-Darren 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Converting DBF to CSV 
Author R.D. Harles 
Date Sep 28, 2007 
Message Darren,

I noticed in your code you use a lower case "f" in the word "false". It needs to be upper case like below. (I tested it and it does make a difference).

workbook.close(False) 
  -----------------
R.D. Harles
ADCi
www.adci.com 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Converting DBF to CSV 
Author Darren McCormick 
Date Oct 01, 2007 
Message Hi R.D.

I tried using an upper-case "F" in false, but I still get the error that "False" is not defined.

Are you using python 2.1?

As mentioned above I also tried using workbook.Close(SaveChanges=0); however, this does not work as previous posts describe. While I don't get an error message with this approach, an Excel popup is generated to ask if I want to save the csv file. My understanding is that SaveChanges=0 should stop this popup from being activated.
 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Converting DBF to CSV 
Author R.D. Harles 
Date Oct 01, 2007 
Message Sorry, I didn't notice you mentioned you were using python 2.1.
I'm using python 2.4.2.
Not sure why there would be a difference though, as it would seem to be an Excel thing, but I don't really know.

Below are the notes from my original script. Apparently the "False" is used to stop the pop-ups as well.
# Close workbook after each save (the False elimiates pop-ups "Do you want to save changes to *.csv?")
workbook.Close(False)

Have you tried to turn off the alerts by using this line:
excel.DisplayAlerts = false 
  -----------------
R.D. Harles
ADCi
www.adci.com 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Converting DBF to CSV 
Author Darren McCormick 
Date Oct 19, 2007 
Message Sorry for taking so long to respond - my spam filter started putting ESRI Forum "Activity on thread" notifications in a junk mail folder.

I tried excel.DisplayAlerts=false, but the "NameError: name 'false' is not defined" is still generated.

-Darren 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Converting DBF to CSV 
Author Gavin McGhie 
Date Aug 07, 2009 
Message Has anyone figured out a reasonable way to convert dbf to csv or any other delimited txt file WITHOUT using Excel? We have a batch process running on our server and don't have an MS Office license for the server... Thanks, Gavin 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Converting DBF to CSV 
Author Gavin McGhie 
Date Aug 07, 2009 
Message Has anyone figured out a reasonable way to convert dbf to csv or any other delimited txt file WITHOUT using Excel? We have a batch process running on our server and don't have an MS Office license for the server... Thanks, Gavin 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Converting DBF to CSV 
Author R.D. Harles 
Date Aug 07, 2009 
Message Gavin,
Have a look at this script...
(It was originally written to process shapefiles, but will work for dbf files as well) 
 
##        
## Convert all values in all fields from a directory full of dbf files to csv text files.
##
## R.D. Harles
## 01-21-09 Original code.
##

import arcgisscripting, os
gp = arcgisscripting.create()

gp.workspace = os.getcwd()

for table in os.listdir(''):
    if table.endswith('dbf'):

        # Make the output file name the same as the shapfile
        outFile = open(table[:-3]+"csv", 'a')        

        ## Append all the field names to the fldList
        fldList = []
        fld = gp.listFields(table)
        field = fld.next()
        while field:
            # Don't include ESRI's "FID" or "SHAPE" or "OID" fields.
            if field.name <> "FID" and field.name <> "Shape" and field.name <> "OID":
                fldList.append(field.name)
            field = fld.next()        

        ## Write the field names to the outFile
        # Get the length of the fldList so we know when not to add the delimiter
        total = len(fldList)
        count = 0
        for fieldName in fldList:            
            count = count+1            
            delimeter = ","
            # If you're not the last value in the list, add the delimiter; else, don't add it
            if not count == total:
                outFile.write(fieldName+delimeter)
            else:
                outFile.write(fieldName+"\n")

        ## Write the row values to the outFile
        rows = gp.SearchCursor(table)
        row = rows.Next()
        while row:
            # Create a list for each row
            valList = []
            for fieldName in fldList:                
                delimeter = ","
                value = row.GetValue(fieldName)
                # Append each row of values to the valList
                valList.append(value)            
            count = 0            
            # Go through the list of values and add the delimiter
            for val in valList:                
                count = count+1
                # If you're not the last value in the list, add the delimiter; else, don't add it
                if not count == total:
                    outFile.write(str(val)+delimeter)
                else:                    
                    outFile.write(str(val)+"\n")
            row = rows.Next()

print "\nDone.\n"
 
  -----------------
R.D. Harles
ADCi
www.adci.com 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Converting DBF to CSV 
Author Gavin McGhie 
Date Aug 07, 2009 
Message Thanks RD! Definitely saves us some coding headache. Too bad ESRI doesn't make that tool avaiable to model builder as it's quite easy in the ArcGIS Gui. We'll give it a try.
cheers, Gavin 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Converting DBF to CSV 
Author Mattias Wallin 
Date Aug 11, 2009 
Message Here is yet another solution...

It doesn't use a geoprocessor so it is quite a bit faster (if it matters).

It uses the standard csv module and the dbfpy module from:
http://dbfpy.sourceforge.net/

Mattias. 
 
import csv
from dbfpy import dbf
try:
    dbfFile = dbf.Dbf(open(r'C:\Data\test\points.dbf','r'))
    csvFile = csv.writer(open(r'C:\Data\test\points.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
 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Converting DBF to CSV 
Author Peter MacKenzie 
Date Aug 26, 2009 
Message How would one implement these conversion scripts within the Model Builder environment. 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Converting DBF to CSV 
Author Geoff Klise 
Date Aug 26, 2009 
Message Here's my implementation in 9.1 from about 3 years ago (with help from R.D. Harles). Not sure if it will help. See the zip file. 
  Geoff Klise
Sandia National Laboratories 
  dbftoshape.zip (opens in new window)
 
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Converting DBF to CSV 
Author Mattias Wallin 
Date Aug 26, 2009 
Message In my case, just modify the file input variables to use gp.GetParameterAsText() instead of being hardcoded.

Open your toolbox in ArcGIS and add the script. Add the appropriate parameters for in/output files in the Parameter tab.

Mattias. 
 
import csv,arcgisscripting
from dbfpy import dbf
gp = arcgisscripting.create()

try:
    inFile = gp.GetParameterAsText(0) #Input
    outFile = gp.GetParameterAsText(1)#Output
    dbfFile = dbf.Dbf(open(inFile,'r'))
    csvFile = csv.writer(open(outFile, '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:
    print gp.getmessage()
 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Converting DBF to CSV 
Author Peter MacKenzie 
Date Aug 27, 2009 
Message That worked nicely thanks.
The only extra work I had to do was add the dbfpy module (i think it mentions this in the forum above)

for python learners like me see...

http://sourceforge.net/projects/dbfpy/
and
http://docs.python.org/install/index.html 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Converting DBF to CSV 
Author Ken Carrier 
Date Nov 20, 2009 
Message When adding the script to a toolbox how do you setup the parameters. I am not having any luck knowing which parameters to set and what to set them as. Any help would be greatly appreciated. 
  Ken Carrier
GIS Specialist
Montgomery County Ohio Water Services