I'm looping through a raster list, to export cell counts (from "Count" field in the table) to a text file, I'm only interested in cases where the field "Value" = 1, so I have included this in my search cursor (inR is from the raster list):
Rows = arcpy.SearchCursor(inR, '"Value" = 1', "", "", "")
However, some rasters in the list do not have value = 1 in the table, in this case, I want to return 0 for this count. I've tried to include this in the following loop through the search cursor
for row in Rows:
countR = row.getValue('Count')
if "countR" not in locals():
countR = 0
#append count to list to export later
value_list.append(countR)
del(countR)
When the search cursor is "empty", the get.Value line does not return any value and the object is not created, however, the following line does not work, the idea is that if countR does not exist, then it will be given the value of 0 for this raster in the list (inR).
Edit: this is where I went wrong, the above is not correct, when the search cursor is empty (like an empty list) , there is no loop and none of the statements within that loop take place.
Any help would be appreciated, or alternatives. Thanks
Value
of anything but 1. If you wanted to retrieve non-1 value rows, you would have to change the search query in your cursor creation.countR
was created, I was repeating values in my list when the raster did not have avalue = 1
, i.e it was taking the previous raster's count value, which was still "in memory"Value = 1
, your if statement will never actually take effect. You would have to create a separate cursor forValue != 1
(or the query equivalent) and then fill all Count fields with 0.