First off, I know there's a better title I could have used so I apologize for the confusion.
I have a short script I've written that takes a some user entered information parcel data a field the calculation will be done for.
# Import arcpy module
import arcpy
# Script arguments
Parcels = arcpy.GetParameterAsText(0)
Field2Calc_for = arcpy.GetParameterAsText(1) #field type derived from Parcels
lengthField = arcpy.GetParameterAsText(2) #field type derived from Parcels
widthField = arcpy.GetParameterAsText(3) #field type derived from Parcels
num = arcpy.GetParameterAsText(2) #Long Type
# Local variables
expression = r'reclass' + str((r'!' + + widthField + r'!', r'!' + lengthField + r'!' , num))
codeblock = """ def reclass(lengthField,widthField,num):
if ((lengthField/num) >= (widthField)):
return 'True'
else:
return '' """
# Process: Calculate Field
arcpy.CalculateField_management(Parcels, Field2Calc_for, expression, "PYTHON", codeblock)
I'm having a difficult time figuring out how to correctly set the string for the expression variable. What I'd like to do is take my user input lengthField & widthField parameters and use variable substitution in the expression variable. I'm having a hard time figuring out if that's possible and what the syntax would look like. The examples for the the CalculateField_management method have the field name hard coded into the script, but I'd like to try to avoid that if possible so the user can choose the field.
I appreciate any help and I'm pretty sure it's just something dumb that I'm overlooking b/c that's usually the case.
Thanks,
Mark
Solved! Go to Solution.
expression = 'reclass(!{0}!,!{1}!,!{2}!)'.format(lengthField, widthField, num)
works with python 2.7
Thanks @RhettZufelt this worked great. I just need to adjust the expression a bit because num was an int type.
expression = 'reclass(!{0}!,!{1}!,{2})'.format(lengthField, widthField, num)
If you are using ArcGIS Pro, which you should be by now, then 7.1.1. Formatted String Literals - Python 3.11.1 documentation are your friend. Try:
expression = f"reclass(!{widthField}!,!{lengthField}!,!{num}!)"
I appreciate the reply. I'm not currently using ArcGIS Pro because I work for a local government and we haven't made the jump to it for editing purposes and a few other reasons, not my decision unfortunately. I'm sure the change is coming soon, but for the time being we're using ArcMap 10.8. Any chance to make the expression work for that. The string literals don't appear to be supported for ArcMap 😞
Thank you
Something like this maybe. Its an old(er) school way (Sorry, I haven't tested it).
expression = "reclass(!%d!,!%d!,!%d!)" % (lengthField, widthField, num)
Thanks Kim, I'll give that a shot.
@KimGarbade I think this probably works too Kim, but I think it would need to be something like this:
"reclass(!%s!,!%s!,%d)" % (lengthField, widthField, num)
expression = 'reclass(!{0}!,!{1}!,!{2}!)'.format(lengthField, widthField, num)
works with python 2.7
Thanks @RhettZufelt this worked great. I just need to adjust the expression a bit because num was an int type.
expression = 'reclass(!{0}!,!{1}!,{2})'.format(lengthField, widthField, num)