0

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.Delete_management('lyr')

    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.Delete_management(output)

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

arcpy.Delete_management('lyr')
2

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'))
c.writerow(['FID','Class'])

#loop through each row and write values to csv
cursor = arcpy.SearchCursor(name of FC)
for row in cursor:
    c.writerow([str(row.getValue(FIDfield)),str(row.getValue(ClassField))])
0

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.