Solved! Go to Solution.
import arcpy, os ws = r'C:\Project\_Forums\Viewshed\test.gdb' # edit this outPath = r'C:\Project\_Forums\Viewshed\testexp2' # edit this outExt = ".csv" arcpy.env.workspace = ws rasters = arcpy.ListRasters("*") for raster in rasters: rasloc = ws + os.sep + raster fields = "*" try: lstFlds = arcpy.ListFields(rasloc) header = '' for fld in lstFlds: header += ",{0}".format(fld.name) if len(lstFlds) != 0: outCSV = outPath + os.sep + raster + outExt f = open(outCSV,'w') header = header[1:] + ',RasterName\n' f.write(header) with arcpy.da.SearchCursor(rasloc, fields) as cursor: for row in cursor: f.write(str(row).replace("(","").replace(")","") + "," + raster + '\n') f.close() except: print raster + " - is not integer or has no attribute table" del row
import arcpy, os ws = r'C:\Project\_Forums\Viewshed\test.gdb' # edit this outPath = r'C:\Project\_Forums\Viewshed\testexp2' # edit this outExt = ".csv" arcpy.env.workspace = ws rasters = arcpy.ListRasters("*") for raster in rasters: rasloc = ws + os.sep + raster fields = "*" try: lstFlds = arcpy.ListFields(rasloc) header = '' for fld in lstFlds: header += ",{0}".format(fld.name) if len(lstFlds) != 0: outCSV = outPath + os.sep + raster + outExt f = open(outCSV,'w') header = header[1:] + ',RasterName\n' f.write(header) with arcpy.da.SearchCursor(rasloc, fields) as cursor: for row in cursor: f.write(str(row).replace("(","").replace(")","") + "," + raster + '\n') f.close() except: print raster + " - is not integer or has no attribute table" del row
Dear Xander,
Thank you so much for the code ! It worked in perfect manner :).
Hi all,
I am new to GIS, I am having large number of raster files(800) in ArcGis and I want to copy the attribute table of each raster file separately to Excel.
I found your link through stack exchange and i want to use your code to perform the above mentioned task, if you can share the code with me as attachment as i try to run the script shared by you, it displays syntax error.
I haven't tested Xander Bakker 's code, but here it is formatted (the old forum conversion scrambled some scripts)
import arcpy, os
ws = r'C:\Project\_Forums\Viewshed\test.gdb'
# edit this outPath = r'C:\Project\_Forums\Viewshed\testexp2'
# edit this outExt = ".csv"
arcpy.env.workspace = ws
rasters = arcpy.ListRasters("*")
for raster in rasters:
rasloc = ws + os.sep + raster
fields = "*"
try:
lstFlds = arcpy.ListFields(rasloc)
header = ''
for fld in lstFlds:
header += ",{0}".format(fld.name)
if len(lstFlds) != 0:
outCSV = outPath + os.sep + raster + outExt
f = open(outCSV,'w')
header = header[1:] + ',RasterName\n'
f.write(header)
with arcpy.da.SearchCursor(rasloc, fields) as cursor:
for row in cursor:
f.write(str(row).replace("(","").replace(")","") + "," + raster + '\n')
f.close()
except:
print raster + " - is not integer or has no attribute table"
del row
If you are just getting into ArcGIS, you should look into Python (if you don't already know it) since that is the scripting language many of us use. Key about python, indentation is very important. Good luck.
Hi all,
The code now works for me but it displays the error message "is not integer or has no attribute table". But all the raster files have the attributes.
Please help
I will share some files with you if required.
Thanks and regards
@Rebecca Strauch, thanks a lot for quick response.
I also corrected Xander's code, it works fine and generates empty .csv files
But the error message 'is not integer or has no attribute table' is displayed.
Thanks again
I'm not a raster expert, but others here could probably help if you post something upload a sample.
In theory everything that goes wrong inside the try except statements will yield that message. I put this message since in most cases the error would be caused by using a floating raster which does not have an attribute table.
You could change the last rows where the exception is caught into this to get more details on the error.
except Exception as e:
print(e)