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.
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
I just realized the data is 32-bit floating point.
Is this an issue?