zia207

Python Script for batch coversion: from DBF to CSV files

Discussion created by zia207 on Aug 22, 2011
Latest reply on Aug 23, 2011 by rafaelr
Following python script (http://forums.esri.com/Thread.asp?c=93&f=1729&t=279777) works well for converstion of a DBF file to a CSV file. But I want to convert 100s of dbf files to cvs files. I need   help  to add a loop in this script to create a list of dbf file in an input  directory("K:\\SSURGO_TABLE\\DATA\\DBF") and save as csv file in a output directory("K:\\SSURGO_TABLE\\DATA\\CSV").
Thanks
Zia


#-------------------------------------
import sys, traceback, os
import arcgisscripting

gp = arcgisscripting.create()

path = r'K:\\SSURGO_TABLE\\DATA\\DBF'
#outWorkspace= r'K:\\SSURGO_TABLE\\DATA\\CSV'
gp.workspace = path
#table = gp.ListTables()
table = 'co_cornmukey.dbf'
csvseparator = ','
outputpath = os.path.join(path,'co_cornmukey.csv')

def print_exception():
    tb = sys.exc_info()[2]
    l = traceback.format_tb(tb)
    l.reverse()
    tbinfo = "".join(l)
    pymsg = "ERROR:\nTraceback Info:\n" + tbinfo + "Error Info:\n    " +  str(sys.exc_type)+ ": " + str(sys.exc_value) + ""
    print pymsg

def get_fieldnames(fields, ignorefields=[]):
    fields_output = []
    for field in iter(fields.next, None):
        if not field.name in ignorefields:
            fields_output.append(field.name)
    return fields_output

try:
    info = gp.describe(table)
    ignore_fields = []
    fieldnames = get_fieldnames(info.fields, ignore_fields)
    print fieldnames
    rows = gp.searchcursor(table)

    output = []
    output.append(csvseparator.join(fieldnames))
   
    for row in iter(rows.next, None):
        outputrow = []
        for fieldname in fieldnames:
            outputrow.append(str(row.getvalue(fieldname)))
        outputrow = csvseparator.join(outputrow)
        output.append(outputrow)
    print 'found', str(len(output)), 'rows'
    f = open(outputpath, 'w')
    f.write('\n'.join(output))
    f.close()
except:
    print_exception()
    gp.getmessages(2)

Outcomes