Select to view content in your preferred language

Can a user input variable be used in an expression variable string for CalculateField_management ?

1005
7
Jump to solution
01-05-2023 08:40 AM
MPach
by
Occasional Contributor II

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

0 Kudos
2 Solutions

Accepted Solutions
RhettZufelt
MVP Notable Contributor

 

expression = 'reclass(!{0}!,!{1}!,!{2}!)'.format(lengthField, widthField, num)

works with python 2.7

 

View solution in original post

MPach
by
Occasional Contributor II

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)

View solution in original post

0 Kudos
7 Replies
JoshuaBixby
MVP Esteemed Contributor

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}!)"
MPach
by
Occasional Contributor II

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

0 Kudos
KimGarbade
Regular Contributor

Something like this maybe. Its an old(er) school way (Sorry, I haven't tested it).

 

 

expression = "reclass(!%d!,!%d!,!%d!)" % (lengthField, widthField, num)

 

 

 

MPach
by
Occasional Contributor II

Thanks Kim, I'll give that a shot. 

0 Kudos
MPach
by
Occasional Contributor II

@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)

 

0 Kudos
RhettZufelt
MVP Notable Contributor

 

expression = 'reclass(!{0}!,!{1}!,!{2}!)'.format(lengthField, widthField, num)

works with python 2.7

 

MPach
by
Occasional Contributor II

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)
0 Kudos