Exporting the attribute tables of rasters in ArcPy

by seak23   Last Updated September 11, 2019 18:22 PM

Like the title suggests I have this script that worked once before to export the attribute tables of some rasters as CSV files. However when I run this script again, I get an error:

Runtime error
Traceback (most recent call last):
   File "<string>", line 28, in <module>
NameError: name 'row' is not defined

I pieced this script together from some thread a while back performing a similar task. Can someone please help?

import arcpy, os
ws = r'INPUT_RASTERS_LOCATION'
outPath = r'OUTPUT_RASTERS_LOCATION'
outExt = "OUTPUT_NAME.csv"
arcpy.env.workspace = ws
rasters = arcpy.ListRasters("*")
for raster in rasters:
    rasloc = os.path.join(ws, raster)
    fields = '*'
    try:
        flds = arcpy.ListFields(rasloc)
        header = ','.join([fld.name for fld in flds])
        if len(flds) != 0:
            outCSV = os.path.join(outPath, '{0}{1}'.format(raster, outExt))
            with open(outCSV,'w') as f:
                header += ',RasterName\n'
                f.write(header)
                curs = arcpy.SearchCursor(rasloc)
                for row in curs:
                    lst = [row.getValue(fld.name) for fld in flds]
                    lst.append(raster)
                    line = ','.join(str(a) for a in lst)
                    f.write(line + '\n')
    except Exception as e:
        print "Error processing", raster
        print "Error", e
        print "Is raster not integer or is there no attribute table?"
del row, curs


Related Questions


Viewing the Attribute Table for a 32 bit Float raster?

Updated September 12, 2019 16:22 PM