6

I am desperately trying to export the values in a raster table to a .txt file. I've seen some other folk post about this, but for some reason can't get the code to work in my case.

I have a raster, with table already built, called "shed". I simply want to export two fields, "Value" and "Count", to a .txt or .csv file (called "try" in this case). So far, I haven't even been able to get a script that even looks at the raster's table. So far I have:

outfile=open("E:/Weathering_GIS/try.txt",'w')
rows=arcpy.SearchCursor('shed',"","","Value;Count","")
for row in rows:
    val=row.getValue('Value')
    count=row.getValue('Count') 
    outfile.write(val)

But "val" and "count" always fail to become anything- they just remain empty values! Am I missing something? Maybe some sort of import I have overlooked?

  • Hi Ryan - First, does an attribute table definitely exist? Secondly, does it have to be in arcpy? You can just try a simple export from the attribute table if you urgently need the data. Thirdly, are you reading in the raster properly? You can't read in the file using the name unless you set the working directly properly using something like env.workspace = r"D:\DATA\\", after the line from arcpy import env – djq Feb 14 '12 at 2:15
6

I think you were missing closing your output text file - until you do that, you won't get anything written to the text file. Below code tested and works:

import arcpy

arcpy.env.workspace = r"C:\Users\chad\Documents\ArcGIS\Default.gdb"

arcpy.BuildRasterAttributeTable_management("raster17", "Overwrite")
outfile = open(r"D:\temp\raster17.txt", "w")

rows = arcpy.SearchCursor("raster17","","","Value;Count","")
for row in rows:
    val = row.getValue("Value")
    count = row.getValue("Count")
    print val, count
    outfile.write(str(val) + "," + str(count) + "\n")

outfile.close()

And it yields this text file:

enter image description here

  • For some reason arcpy.SearchCursor still gave me empty lists, but arcpy.da.SearchCursor gave me the correct result. Weird! – mikeck Nov 18 '16 at 21:56
1

I don't think you need arcpy for this. It's be much easier to just export the attribute table to text as celenius suggested.. You can export a raster attribute table in ArcMap or ArcCatalog. You need to be viewing the raster attribute table to do this though. From the ArcGIS help page:

  1. Click the Options button and click Export.
  2. Click the Export drop-down arrow on the Export Data dialog box to choose to export All records or Selected records. The Selected records option is only available if records are selected in the table.
  3. Click the browse button and navigate to the folder or geodatabase in which you want to place the exported data.
  4. Click the Save as type drop-down arrow and click the format to which you want to export the data.
  5. Type a name for the exported table.
  6. Click Save.
  7. Click OK.

This assumes that you already have a raster attribute table. If you don't have it yet, you can build it using the Build Raster Attribute tool.

  • Celenius- thanks; I'll look into those tips. I did have the workspace set, though... R. K.-That'd be fine if I didn't have to do hundreds of these. I'm trying to get something that I can use for batch-processing. – Ryan Utz Feb 14 '12 at 15:33
  • Your original question didn't really say anything about batch-processing. Anyways, good to know that you found your solution. – R.K. Feb 15 '12 at 1:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.