Select to view content in your preferred language

variable output file names

1970
7
Jump to solution
05-02-2013 12:18 PM
toddsams
Deactivated User
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)
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
Luke_Pinner
MVP Regular Contributor
The error message tells you the problem, you can't assign the result of an expression to another expression, only to a variable name.
Bad:
fac + i = ExtractByAttributes("Fac", "Value =" + i)

Good:
#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))


Note the variable name is completely unrelated to the output file name... Are you saving the output to a file? If so, there's no reason to try and get the "i" value in the variable name, just use it in the output file name:
extracted = ExtractByAttributes("Fac", "Value = %s" % i) extracted.save("D:/data/output/fac"+str(i))

View solution in original post

0 Kudos
7 Replies
JamesCrandall
MVP Frequent Contributor
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)


edit: I see my solution wouldn't work....

I don't quite understand why you are doing this:


fac + i




Just convert the i value to a string:

## updated
fac + str(i) = ExtractByAttributes("Fac", "Value =" + str(i))

0 Kudos
toddsams
Deactivated User
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)
0 Kudos
JamesCrandall
MVP Frequent Contributor
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)


post more of the code as it is hard to tell what fac is.
0 Kudos
toddsams
Deactivated User
fac is not a variable. It is just a string I would like to use as the front end of my output file names. I'd like the file names to read fac1111, fac2222, fac3333 etc.

I tried putting fac in quotes as: "fac" + str(i) but this did not work either.

>>> 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)
0 Kudos
JoelCalhoun
Deactivated User
I've never used ExtractByAttributes but after a quick scan of the help you could maybe try something like this:

# 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


You have "fac" and "Fac" in your code.  Python will treat these differently.
My understanding is that "fac" is just a string you want as part of the output file name.

Is "Fac" with the capital letter the name of your input raster?

In my code sample I just made the both caps because I am unsure what your input data is.
0 Kudos
Luke_Pinner
MVP Regular Contributor
The error message tells you the problem, you can't assign the result of an expression to another expression, only to a variable name.
Bad:
fac + i = ExtractByAttributes("Fac", "Value =" + i)

Good:
#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))


Note the variable name is completely unrelated to the output file name... Are you saving the output to a file? If so, there's no reason to try and get the "i" value in the variable name, just use it in the output file name:
extracted = ExtractByAttributes("Fac", "Value = %s" % i) extracted.save("D:/data/output/fac"+str(i))
0 Kudos
toddsams
Deactivated User
Thanks both Joel and Luke. This works, and now I have the concept of naming the file in the save function.

-todd
0 Kudos