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?
2 Answers 2
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, butarcpy.da.SearchCursor
gave me the correct result. Weird!mikeck– mikeck2016年11月18日 21:56:43 +00:00Commented Nov 18, 2016 at 21:56
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:
- Click the Options button and click Export.
- 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.
- Click the browse button and navigate to the folder or geodatabase in which you want to place the exported data.
- Click the Save as type drop-down arrow and click the format to which you want to export the data.
- Type a name for the exported table.
- Click Save.
- 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– Ryan Utz2012年02月14日 15:33:20 +00:00Commented 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.R.K.– R.K.2012年02月15日 01:27:43 +00:00Commented Feb 15, 2012 at 1:27
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 likeenv.workspace = r"D:\DATA\\"
, after the linefrom arcpy import env