1

Trying to use the ExtractByAttributes for rasters so that I have multiple attribute values that I want to extract.

This works fine: myraster = arcpy.sa.ExtractByAttributes("source_raster", "VALUE IN (1,8,10)")

but obviously I can't repeat this procedure for hundreds of layers by manually inserting the desired attribute values for each. Anything I've tried for replacing the numbers with a variable has not worked. It doesn't seem to take a list of strings or integers, nor does it help to strip the brackets[] from around the list, or to have the numbers as a single string. I've even tried with a single number instead of many and I just can't get it to work without inserting the actual number in there. What on earth am I missing? Needless to say I'm a beginner with Python.

Many thanks for any ideas

2

The probelm is that arcpy is expecting a single string, including the , and (). in your case this should look like :

myraster = arcpy.sa.ExtractByAttributes("source_raster", "VALUE IN (" str(val1)  +"," + str(val2) + "," + str(val3) +")")

where val1,2 and 3 are integers.

or you can convert your list to a string before

s_mylist = "("
for i in mylist:
    s_mylist += (str(i) + ",")
s_mylist = s_mylist[:-1]+")"
myraster = arcpy.sa.ExtractByAttributes("source_raster", "VALUE IN " + s_mylist)
  • Wow, I thought I had tried to make it into a single string already but must have made some mistake along the way with parenthesis or sth because now with your code it seems to be working! HUGE THANKS! – dummy Mar 5 '14 at 15:04

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.