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?

Chad Cooper
12.8k4 gold badges48 silver badges87 bronze badges
asked Feb 13, 2012 at 23:39
1
  • 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 Commented Feb 14, 2012 at 2:15

2 Answers 2

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

answered Feb 14, 2012 at 15:52
1
  • For some reason arcpy.SearchCursor still gave me empty lists, but arcpy.da.SearchCursor gave me the correct result. Weird! Commented Nov 18, 2016 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.

answered Feb 14, 2012 at 2:52
2
  • 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. Commented Feb 14, 2012 at 15:33
  • Your original question didn't really say anything about batch-processing. Anyways, good to know that you found your solution. Commented Feb 15, 2012 at 1:27

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.