Export values in a raster table to .txt or .csv file

4809
1
02-13-2012 10:55 AM
RyanUtz
Emerging Contributor
Hello,

I'm batch-processing some watershed stuff and wish to automatically export the values in a raster table to anything useful outside of GIS, such as a .txt, .csv or .xls file. I cannot locate this command. It was once upon a time quite easy with Arc/INFO but those days have passed. Also, the ability to append to the output file (rather than write 100s of different files) would be very nice.

Any advice?

Thanks!
Ryan
Tags (2)
0 Kudos
1 Reply
ChrisSnyder
Honored Contributor
1. The CopyRows tool will export a raster attribute table to any "ESRI-approved" table format (.dbf, .mdb., .gdb, SDE, INFO, etc.) Unfortunatly, tab and comma delimited text format tables are not in that list! You would need to write your own thing to do that via Python (pretty easy to do), or download someone elses toolbox/code... Maybe a toolbox/python code such as http://arcscripts.esri.com/details.asp?dbid=13692?
Curious that you can do a right click and "Export As" in ArcMap/ArcCatalog and export to a txt format (tab delimited BTW). However, there doesn't seem to be a tool in ArcToolbox that can accomplish this amazing feat...

2. Do you mean append to the table? Again, if you use one of the ESRI-appropved table formats, the Append tool works just great. There doesn't appear to be a way to do this "out of the box" however if you are using a txt fomat table though!

A good resource for basic file reading/writting in Python: http://docs.python.org/tutorial/inputoutput.html
A good resource for more "precise" csv file writting: http://docs.python.org/library/csv.html

Here's some basic code I wrote to build a quick and dirty comma delimited text file:
import arcpy 
outputTxtFile = r"C:\csny490\myfile.txt" #output comma delimited text file
myGrid = r"C:\csny490\mm_from_space\wa06_mean4bin" #input raster file
fieldList = arcpy.ListFields(myGrid) #make a list of the fields
f = open(outputTxtFile, 'w') #open the text file for writting
f.write(",".join(['"'+ field.name + '"' for field in fieldList]) + "\n") #write the header (field names) using list comprehension 
searchRows = arcpy.SearchCursor(myGrid) #open a search cursor
for searchRow in searchRows: #loop through the rows
    f.write(",".join([str(searchRow.getValue(field.name)) for field in fieldList]) + "\n") #write out the field values of each row using list comprehension 
del searchRow, searchRows #delete the cursor objects
f.close() #close the text file
0 Kudos