1

This is a working code made using exporting the script from ModelBuilder.

import arcpy

# Local variables:
Links_Final = "Links_Final"
condition = "\"RR1\"= 777 OR \"RR2\"=777 OR \"RR3\"=777 OR \"RR4\"=777"

# Process: Select Layer By Attribute
arcpy.SelectLayerByAttribute_management(Links_Final, "NEW_SELECTION", condition)

However, the I want the value 777 in the condition to be taken from the user. So I added this code.

value = int(arcpy.GetParameterAsText(0)) 
condition = "\"RR1\"= value OR \"RR2\"= value OR \"RR3\"= value OR \"RR4\"= value"

Now the code throws an error saying, Invalid expression Failed to execute (SelectLayerByAttribute)

I have also followed the thread https://stackoverflow.com/questions/32483187/trying-to-create-a-simple-select-by-attribute-script-based-on-an-input but that does not work either.

Note: My data type on the field is long integer and the data type taken from the user (as specified while creating the script) was long (as I could not find int type there)

2

Use the python String Format to pass parameters into a string variable. Also triple quote your string so that you don't have to escape each internal quote mark.

import arcpy

# Local variables:
Links_Final = "Links_Final"
value = int(arcpy.GetParameterAsText(0)) 

condition = """ "RR1" = {0} OR "RR2" = {0} OR "RR3" = {0} OR "RR4" = {0} """.format(value)

# Process: Select Layer By Attribute
arcpy.SelectLayerByAttribute_management(Links_Final, "NEW_SELECTION", condition)

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.