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
Looking at the docs for the search cursor, it says that raster fields are not supported:
"Raster fields are not supported."
What does your code look like? ... It would probably be best to create a new topic for your question.
Got the aswner in https://gis.stackexchange.com/questions/335604/exporting-the-attribute-tables-of-rasters-in-arcpy
Hello! First of all, sorry to revive an old post.
I'm also new to GIS, and needed something similar to this post, but I'm getting only one row of data from my rasters attribute tables.
The script which I'm replying to is creating the CSV files, generating two from four headers (creates objectid and rastername, but not "value" and "count", which are other columns from my attribute table) and writes just the first line of data from my rasters. I attached one example.
Also, it would be better for me to all this data got into one file, is that possible?
Could someone help?
Thanks!
the csv module handles way more cases and error checks to its toolset. If you have a well know data structure and you will be using it, then simple is good, should there be a chance that others may be doing it perhaps with poorly formed data, then the csv module is worth a look, just to save writing error checking.
Is there an advantage to using the csv module over just creating a file with .csv extension and writing lines to it like you did in this example? Your way seems much simpler.
xander_bakker, Thanks a lot for the code, it worked perfectly.
Thanks again for saving my time.
Regards
John
I changed the code to this and that resulted in csv files with data.
<SPAN class="keyword token">import</SPAN> arcpy<SPAN class="punctuation token">,</SPAN> os ws <SPAN class="operator token">=</SPAN> r<SPAN class="string token">'D:\Xander\GeoNet\Raster2CSV\data'</SPAN> outPath <SPAN class="operator token">=</SPAN> r<SPAN class="string token">'D:\Xander\GeoNet\Raster2CSV\csv'</SPAN> outExt <SPAN class="operator token">=</SPAN> <SPAN class="string token">".csv"</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>workspace <SPAN class="operator token">=</SPAN> ws rasters <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>ListRasters<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"*"</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">for</SPAN> raster <SPAN class="keyword token">in</SPAN> rasters<SPAN class="punctuation token">:</SPAN> rasloc <SPAN class="operator token">=</SPAN> os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>join<SPAN class="punctuation token">(</SPAN>ws<SPAN class="punctuation token">,</SPAN> raster<SPAN class="punctuation token">)</SPAN> fields <SPAN class="operator token">=</SPAN> <SPAN class="string token">'*'</SPAN> <SPAN class="keyword token">try</SPAN><SPAN class="punctuation token">:</SPAN> flds <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>ListFields<SPAN class="punctuation token">(</SPAN>rasloc<SPAN class="punctuation token">)</SPAN> header <SPAN class="operator token">=</SPAN> <SPAN class="string token">','</SPAN><SPAN class="punctuation token">.</SPAN>join<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">[</SPAN>fld<SPAN class="punctuation token">.</SPAN>name <SPAN class="keyword token">for</SPAN> fld <SPAN class="keyword token">in</SPAN> flds<SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">if</SPAN> len<SPAN class="punctuation token">(</SPAN>flds<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">!=</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">:</SPAN> outCSV <SPAN class="operator token">=</SPAN> os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>join<SPAN class="punctuation token">(</SPAN>outPath<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'{0}{1}'</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>raster<SPAN class="punctuation token">,</SPAN> outExt<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">with</SPAN> open<SPAN class="punctuation token">(</SPAN>outCSV<SPAN class="punctuation token">,</SPAN><SPAN class="string token">'w'</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">as</SPAN> f<SPAN class="punctuation token">:</SPAN> header <SPAN class="operator token">+=</SPAN> <SPAN class="string token">',RasterName\n'</SPAN> f<SPAN class="punctuation token">.</SPAN>write<SPAN class="punctuation token">(</SPAN>header<SPAN class="punctuation token">)</SPAN> curs <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>SearchCursor<SPAN class="punctuation token">(</SPAN>rasloc<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">for</SPAN> row <SPAN class="keyword token">in</SPAN> curs<SPAN class="punctuation token">:</SPAN> lst <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN>row<SPAN class="punctuation token">.</SPAN>getValue<SPAN class="punctuation token">(</SPAN>fld<SPAN class="punctuation token">.</SPAN>name<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">for</SPAN> fld <SPAN class="keyword token">in</SPAN> flds<SPAN class="punctuation token">]</SPAN> lst<SPAN class="punctuation token">.</SPAN>append<SPAN class="punctuation token">(</SPAN>raster<SPAN class="punctuation token">)</SPAN> line <SPAN class="operator token">=</SPAN> <SPAN class="string token">','</SPAN><SPAN class="punctuation token">.</SPAN>join<SPAN class="punctuation token">(</SPAN>str<SPAN class="punctuation token">(</SPAN>a<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">for</SPAN> a <SPAN class="keyword token">in</SPAN> lst<SPAN class="punctuation token">)</SPAN> f<SPAN class="punctuation token">.</SPAN>write<SPAN class="punctuation token">(</SPAN>line <SPAN class="operator token">+</SPAN> <SPAN class="string token">'\n'</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">except</SPAN> Exception <SPAN class="keyword token">as</SPAN> e<SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">print</SPAN> <SPAN class="string token">"Error processing"</SPAN><SPAN class="punctuation token">,</SPAN> raster <SPAN class="keyword token">print</SPAN> <SPAN class="string token">"Error"</SPAN><SPAN class="punctuation token">,</SPAN> e <SPAN class="keyword token">print</SPAN> <SPAN class="string token">"Is raster not integer or is there no attribute table?"</SPAN> <SPAN class="keyword token">del</SPAN> row<SPAN class="punctuation token">,</SPAN> curs<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
See files attached.
@Xander Bakker , the code runs fine for me and also generates .csv files but they contain only headers.
I am attaching a copy of *.csv files generated and some sample files.
Please help
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.
<SPAN class="keyword token">except</SPAN> Exception <SPAN class="keyword token">as</SPAN> e<SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN>e<SPAN class="punctuation token">)</SPAN> <SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN></SPAN>
I'm not a raster expert, but others here could probably help if you post something upload a sample.
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
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.
I will share some files with you if required.
Thanks and regards
I haven't tested Xander Bakker 's code, but here it is formatted (the old forum conversion scrambled some scripts)
<SPAN class="keyword token">import</SPAN> arcpy<SPAN class="punctuation token">,</SPAN> os ws <SPAN class="operator token">=</SPAN> r<SPAN class="string token">'C:\Project\_Forums\Viewshed\test.gdb'</SPAN> <SPAN class="comment token"># edit this outPath = r'C:\Project\_Forums\Viewshed\testexp2' </SPAN> <SPAN class="comment token"># edit this outExt = ".csv" </SPAN> arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>workspace <SPAN class="operator token">=</SPAN> ws rasters <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>ListRasters<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"*"</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">for</SPAN> raster <SPAN class="keyword token">in</SPAN> rasters<SPAN class="punctuation token">:</SPAN> rasloc <SPAN class="operator token">=</SPAN> ws <SPAN class="operator token">+</SPAN> os<SPAN class="punctuation token">.</SPAN>sep <SPAN class="operator token">+</SPAN> raster fields <SPAN class="operator token">=</SPAN> <SPAN class="string token">"*"</SPAN> <SPAN class="keyword token">try</SPAN><SPAN class="punctuation token">:</SPAN> lstFlds <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>ListFields<SPAN class="punctuation token">(</SPAN>rasloc<SPAN class="punctuation token">)</SPAN> header <SPAN class="operator token">=</SPAN> <SPAN class="string token">''</SPAN> <SPAN class="keyword token">for</SPAN> fld <SPAN class="keyword token">in</SPAN> lstFlds<SPAN class="punctuation token">:</SPAN> header <SPAN class="operator token">+=</SPAN> <SPAN class="string token">",{0}"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>fld<SPAN class="punctuation token">.</SPAN>name<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">if</SPAN> len<SPAN class="punctuation token">(</SPAN>lstFlds<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">!=</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">:</SPAN> outCSV <SPAN class="operator token">=</SPAN> outPath <SPAN class="operator token">+</SPAN> os<SPAN class="punctuation token">.</SPAN>sep <SPAN class="operator token">+</SPAN> raster <SPAN class="operator token">+</SPAN> outExt f <SPAN class="operator token">=</SPAN> open<SPAN class="punctuation token">(</SPAN>outCSV<SPAN class="punctuation token">,</SPAN><SPAN class="string token">'w'</SPAN><SPAN class="punctuation token">)</SPAN> header <SPAN class="operator token">=</SPAN> header<SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">:</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">+</SPAN> <SPAN class="string token">',RasterName\n'</SPAN> f<SPAN class="punctuation token">.</SPAN>write<SPAN class="punctuation token">(</SPAN>header<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">with</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>da<SPAN class="punctuation token">.</SPAN>SearchCursor<SPAN class="punctuation token">(</SPAN>rasloc<SPAN class="punctuation token">,</SPAN> fields<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">as</SPAN> cursor<SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">for</SPAN> row <SPAN class="keyword token">in</SPAN> cursor<SPAN class="punctuation token">:</SPAN> f<SPAN class="punctuation token">.</SPAN>write<SPAN class="punctuation token">(</SPAN>str<SPAN class="punctuation token">(</SPAN>row<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>replace<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"("</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">""</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>replace<SPAN class="punctuation token">(</SPAN><SPAN class="string token">")"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">""</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">+</SPAN> <SPAN class="string token">","</SPAN> <SPAN class="operator token">+</SPAN> raster <SPAN class="operator token">+</SPAN> <SPAN class="string token">'\n'</SPAN><SPAN class="punctuation token">)</SPAN> f<SPAN class="punctuation token">.</SPAN>close<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">except</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">print</SPAN> raster <SPAN class="operator token">+</SPAN> <SPAN class="string token">" - is not integer or has no attribute table"</SPAN> <SPAN class="keyword token">del</SPAN> row <SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
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.
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.
Dear Xander,Thank you so much for the code ! It worked in perfect manner .
Membros conectados podem postar, seguir atualizações e mais. Novo aqui? Registre uma conta gratuita.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.