>>> while i < 111101: ... i = 1111 ... fac + i = ExtractByAttributes("Fac", "Value =" + i) ... i = i + 1111 ... Runtime error SyntaxError: can't assign to operator (<string>, line 3)
Solved! Go to Solution.
fac + i = ExtractByAttributes("Fac", "Value =" + i)
#Use a list or a dict fac={} fac = ExtractByAttributes("Fac", "Value =" + i) #Or use the eval function to evaluate a string expression (not as good...) eval('fac'+str(i)+'=ExtractByAttributes("Fac", "Value = ' + str(i) +'"') #Or eval using string format codes eval('fac%s=ExtractByAttributes("Fac", "Value = %s"' % (i,i))
extracted = ExtractByAttributes("Fac", "Value = %s" % i) extracted.save("D:/data/output/fac"+str(i))
I am running a while loop and would like to name each output file using the iterator (i) as part of the file name. I have tried "fac" + i, "fac" + str(i), fac + str(i), and fac + i. None of these work.
Does anyone know how this can be done?>>> while i < 111101: ... i = 1111 ... fac + i = ExtractByAttributes("Fac", "Value =" + i) ... i = i + 1111 ... Runtime error SyntaxError: can't assign to operator (<string>, line 3)
fac + i
## updated fac + str(i) = ExtractByAttributes("Fac", "Value =" + str(i))
>>> while i < 111101: ... i = 1111 ... fac + i = ExtractByAttributes("Fac", "Value =" + str(i)) ... i = i + 1111 ... Runtime error SyntaxError: can't assign to operator (<string>, line 3)
I still get the error. I think the problem is with the fac + 1 at the beginning of line 3. I have tried using str(i) there too, and that does not help.>>> while i < 111101: ... i = 1111 ... fac + i = ExtractByAttributes("Fac", "Value =" + str(i)) ... i = i + 1111 ... Runtime error SyntaxError: can't assign to operator (<string>, line 3)
>>> while i < 111101: ... i = 1111 ... "fac" + str(i) = ExtractByAttributes("Fac", "Value =" + str(i)) ... i = i + 1111 ... Runtime error SyntaxError: can't assign to operator (<string>, line 3)
# Import system modules import arcpy, os from arcpy import env from arcpy.sa import * env.workspace = r"C:\rasters" # Whatever your workspace is outPath = r"C:\RasterExtracts" #Output Location for your raster attribute extract i = 1111 # Moved this outside the loop so you don't get an infinite loop while i < 111101: sqlClause = "Value = " + str(i) outRaster = ExtractByAttributes("Fac", sqlClause) outRaster.save(outPath + os.sep + "Fac" + str(i)) i += 1111
fac + i = ExtractByAttributes("Fac", "Value =" + i)
#Use a list or a dict fac={} fac = ExtractByAttributes("Fac", "Value =" + i) #Or use the eval function to evaluate a string expression (not as good...) eval('fac'+str(i)+'=ExtractByAttributes("Fac", "Value = ' + str(i) +'"') #Or eval using string format codes eval('fac%s=ExtractByAttributes("Fac", "Value = %s"' % (i,i))
extracted = ExtractByAttributes("Fac", "Value = %s" % i) extracted.save("D:/data/output/fac"+str(i))