This is a continuation of a problem that was solved earlier this week. The code below performs a select by location and was originally intended to export all feature classes from the SDE database to shapefiles. However, since the SDE uses coded calues, there is an output issue when using CopyFeatures_management.

The work around that was proposed was to create export to csv/xls, but the TableToExcel function only explicitly accepts tables in arcpy. Someone recommended using arcpy.Describe but how could I take what it reads and convert it to a table? Or is there another solution to this?

import arcpy
from arcpy import env

env.workspace = r'C:\ArcScratch\HM_Production.sde'

stateFeature = r'C:\ArcScratch\State\Parishes_LDOTD_2007.shp'
parish = "Lafayette"
nameField = "PARISH"

arcpy.MakeFeatureLayer_management(stateFeature, 'SelectionStateLayer', '"' +     
str(nameField) + '" = ' + "'" + str(parish) + "'")

for fc in arcpy.ListFeatureClasses():

    if arcpy.Exists('lyr'): #cleanup check

    arcpy.MakeFeatureLayer_management(fc, 'lyr') #converts feature class to lyr
    arcpy.SelectLayerByLocation_management('lyr', "WITHIN", 'SelectionStateLayer') #selection using parish layer

    outname = fc.replace(".","_") #replaces all . with _
    outpath = r"C:\output\{0}".format(outname)

    if arcpy.Exists(outpath): #cleanup check

    arcpy.TableToExcel_conversion('lyr', outpath) #output selected features


If you just need a table with in the Arc environment You could use arcpy.MakeTableView_management. Other wise you can use the cursor to write the file to a csv file using whatever values you want written to the file.

import csv
c = csv.writer(open(outputcsv,'wb'))

#loop through each row and write values to csv
cursor = arcpy.SearchCursor(name of FC)
for row in cursor:

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.