Help with Expression Syntax with two variables - SelectLayerByAttribute.

773
5
Jump to solution
06-07-2013 09:08 AM
JohnLay
Occasional Contributor
I am trying to create and expression that uses the value of the ELEVATION field in a feature class to select out a single feature and export it to its own feature class; essentially "ELEVATION" = 3 where the value '3' is a variable.

Needless to say, I'm having difficulty with the expression--I don???t have a firm grasp on the whole "\""  thing.

Here's what I have at the moment.

field = "ELEVATION" cursor = arcpy.SearchCursor(GAGELayer) for row in cursor:     ElValue = float(row.getValue(field))     exp = "\"" + field + "\" = " + GridValue + ""\"     arcpy.SelectLayerByAttribute_management (GAGEMergeLayer, "NEW_SELECTION", exp)


I get it if '3' was a constant. (I'm not real clear what "\"" means exactly. does "\"" = "" or = "? And how do you reverse it for the end of an expression?)

Something like this

exp = "\"" + field + "\" = 3"


I'm not sure how to take that and make the expression work with '3' as a variable.

Any help (and a little explanation) would be greatly appreciated.
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
by Anonymous User
Not applicable
Is your GridValue a string?  You only need to put string values inside the single quotes.  If it is a number you do not need to put it inside the single quotes.

So if it is a string it would be:

"ELEVATION" = '3'

If an integer:

"ELEVATION" = 3

As for all the slashes and such those are for escaping.

You can use the backslashes to escape single quotes like

exp = ' "Elevation" = \'3\' '


Or you can use the triple quote method

exp = """ "Elevation" = '3' """


I do not think this will matter too much in this case because I am guessing your grid value is an integer and therefore the first sample below should do it (although I cannot see your GridValue variable)

field = "ELEVATION" cursor = arcpy.SearchCursor(GAGELayer) for row in cursor:     ElValue = float(row.getValue(field))     exp = '"{0}" = {1}'.format(field, GridValue)     arcpy.SelectLayerByAttribute_management (GAGEMergeLayer, "NEW_SELECTION", exp) 


if the value is indeed a string, the safest way is to use the Add" rel="nofollow" target="_blank">http://resources.arcgis.com/en/help/main/10.1/index.html#//018v0... Field Delimiters

field = arcpy.AddFieldDelimiters(GAGELayer, "ELEVATION") cursor = arcpy.SearchCursor(GAGELayer) for row in cursor:     ElValue = float(row.getValue(field))     exp = """ {0} = '{1}' """.format(field, GridValue)     arcpy.SelectLayerByAttribute_management (GAGEMergeLayer, "NEW_SELECTION", exp) 

View solution in original post

0 Kudos
5 Replies
by Anonymous User
Not applicable
Is your GridValue a string?  You only need to put string values inside the single quotes.  If it is a number you do not need to put it inside the single quotes.

So if it is a string it would be:

"ELEVATION" = '3'

If an integer:

"ELEVATION" = 3

As for all the slashes and such those are for escaping.

You can use the backslashes to escape single quotes like

exp = ' "Elevation" = \'3\' '


Or you can use the triple quote method

exp = """ "Elevation" = '3' """


I do not think this will matter too much in this case because I am guessing your grid value is an integer and therefore the first sample below should do it (although I cannot see your GridValue variable)

field = "ELEVATION" cursor = arcpy.SearchCursor(GAGELayer) for row in cursor:     ElValue = float(row.getValue(field))     exp = '"{0}" = {1}'.format(field, GridValue)     arcpy.SelectLayerByAttribute_management (GAGEMergeLayer, "NEW_SELECTION", exp) 


if the value is indeed a string, the safest way is to use the Add" rel="nofollow" target="_blank">http://resources.arcgis.com/en/help/main/10.1/index.html#//018v0... Field Delimiters

field = arcpy.AddFieldDelimiters(GAGELayer, "ELEVATION") cursor = arcpy.SearchCursor(GAGELayer) for row in cursor:     ElValue = float(row.getValue(field))     exp = """ {0} = '{1}' """.format(field, GridValue)     arcpy.SelectLayerByAttribute_management (GAGEMergeLayer, "NEW_SELECTION", exp) 
0 Kudos
JohnLay
Occasional Contributor
Caleb,

Thanks for you reply.

Yes GridValue is and integer--actually it will most likely be a float value or either or: i.e. ElValue can equal 118, or 118.5, etc.

The code should actually look like this:

field = "ELEVATION"
cursor = arcpy.SearchCursor(GAGELayer)
for row in cursor:
    ElValue = float(row.getValue(field))
    exp = "\"" + field + "\" = " + ElValue + ""\"
    arcpy.SelectLayerByAttribute_management (GAGEMergeLayer, "NEW_SELECTION", exp)


I tried

exp = '"{0}" = {1}'.format(field, GridValue)


and I'm still getting:

ExecuteError: ERROR 000358: Invalid expression
0 Kudos
JamesCrandall
MVP Frequent Contributor
Caleb,

Thanks for you reply.

Yes GridValue is and integer--actually it will most likely be a float value.

The code should actually look like this:

field = "ELEVATION"
cursor = arcpy.SearchCursor(GAGELayer)
for row in cursor:
    ElValue = float(row.getValue(field))
    exp = "\"" + field + "\" = " + ElValue + ""\"
    arcpy.SelectLayerByAttribute_management (GAGEMergeLayer, "NEW_SELECTION", exp)


I tried

exp = '"{0}" = {1}'.format(field, GridValue)


and I'm still getting:

ExecuteError: ERROR 000358: Invalid expression


Could it be because ElValue is an integer and you are attempting to concatenate it into a string?  Maybe try changing this line:

exp = "\"" + field + "\" = " + str(ElValue) + "\"" 
0 Kudos
by Anonymous User
Not applicable
James is right, if ElValue is what you need to pass in (you had GridValue in the first post).

Although, I do not think all the convoluted backslashes are necessary.  The expression can simply be:

exp = ' "ELEVATION" = %s '  %ElValue

# OR 

exp = ' "ELEVATION" =  '   + ElValue



Try this:

field = "ELEVATION"
cursor = arcpy.SearchCursor(GAGELayer)
for row in cursor:
    ElValue = float(row.getValue(field))
    exp = '"{0}" = {1}'.format(field, ElValue)
    arcpy.SelectLayerByAttribute_management (GAGEMergeLayer, "NEW_SELECTION", exp) 

0 Kudos
JohnLay
Occasional Contributor
Caleb,

Your suggestion

exp = '"{0}" = {1}'.format(field, ElValue)


worked after I finally figured out else I was doing wrong. It helps if the search and select from layer are the same layer. DOH!

Thank you as well, James.

Now off to find the rest of the bugs in this beast.
0 Kudos