Export raster attribute table into CSV using Python automation !

9967
17
Jump to solution
11-25-2013 05:25 PM
YamMagar
New Contributor II
Hello,

I am learning python scripting. I am trying to export raster attribute table (integer value) in to .CSV file. I can manually export attribute table into .txt file but would take many hours to export for 100 raster files. I have raster files (about 100) organized in a folder called "temperature". So, how can I write a python script that scan through all raster files and export attribute table of each raster into .CSV file with unique header file.

Thanks.
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
XanderBakker
Esri Esteemed Contributor
Hi Milan,

Try the code below. Edit the following locations:

  • ws = this is the input workspace (folder) with all he rasters

  • outPath = is the folder where the output CSV files will be written

I have added the raster name as an additional field to the CSV.

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


Example output CSV file:
OBJECTID,Value,Count,RasterName
1, 0, 1162115.0,Viewshed7
2, 1, 62868.0,Viewshed7

Kind regards,

Xander

View solution in original post

0 Kudos
17 Replies
XanderBakker
Esri Esteemed Contributor
Hi Milan,

Try the code below. Edit the following locations:

  • ws = this is the input workspace (folder) with all he rasters

  • outPath = is the folder where the output CSV files will be written

I have added the raster name as an additional field to the CSV.

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


Example output CSV file:
OBJECTID,Value,Count,RasterName
1, 0, 1162115.0,Viewshed7
2, 1, 62868.0,Viewshed7

Kind regards,

Xander
0 Kudos
YamMagar
New Contributor II
Dear Xander,

Thank you so much for the code ! It worked in perfect manner :).
0 Kudos
XanderBakker
Esri Esteemed Contributor
Dear Xander,

Thank you so much for the code ! It worked in perfect manner :).


Hi Milan,

I'm glad I could be of any assistance. If you think it was helpful you    can use the "arrow" button in order to help other  members find useful   information:



More info here: http://resources.arcgis.com/en/help/forums-mvp/

Kind regards,

Xander
0 Kudos
JohnWani
New Contributor

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.

0 Kudos
RebeccaStrauch__GISP
MVP Emeritus

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.

JohnWani
New Contributor

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

0 Kudos
JohnWani
New Contributor

@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

0 Kudos
RebeccaStrauch__GISP
MVP Emeritus

I'm not a raster expert, but others here could probably help if you post something upload a sample.

0 Kudos
XanderBakker
Esri Esteemed Contributor

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)