I am working on a simple application. The user can choose either Numeric values or string values to select (i.e. valName). However if you write the where clause like this:
arcpy.SelectLayerByAttribute_management(lyr, "NEW_SELECTION", FieldName + opName + "'" + valName + "'")
It doesn't work for numbers and if you write like this:
arcpy.SelectLayerByAttribute_management(lyr, "NEW_SELECTION",FieldName + opName + valName)
It doesn't work for strings.
Any simple solution without if condition?
Solved! Go to Solution.
This worked for me:
if type(valName) is str or type(valName) is unicode:
whereClause = FieldName + opName + "'" + valName + "'"
else:
whereClause = FieldName + opName + str(valName)
arcpy.SelectLayerByAttribute_management(lyr, "NEW_SELECTION", whereClause)
"{} {} '{}'".format(FieldName,opName,valName) if type(valName) is str else "{} {} {}".format(FieldName,opName,valName)
You might try something like:
This worked for me:
if type(valName) is str or type(valName) is unicode:
whereClause = FieldName + opName + "'" + valName + "'"
else:
whereClause = FieldName + opName + str(valName)
arcpy.SelectLayerByAttribute_management(lyr, "NEW_SELECTION", whereClause)
You can also use "in"
if type(valName) in (str, unicode) else ...