<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Help with Expression Syntax with two variables - SelectLayerByAttribute. in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/help-with-expression-syntax-with-two-variables/m-p/549133#M42881</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Is your GridValue a string?&amp;nbsp; You only need to put string values inside the single quotes.&amp;nbsp; If it is a number you do not need to put it inside the single quotes.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;So if it is a string it would be:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;"ELEVATION" = '3'&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;If an integer:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;"ELEVATION" = 3&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;As for all the slashes and such those are for escaping.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;You can use the backslashes to escape single quotes like &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;exp = ' "Elevation" = \'3\' '&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Or you can use the triple quote method&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;exp = """ "Elevation" = '3' """&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;field = "ELEVATION" cursor = arcpy.SearchCursor(GAGELayer) for row in cursor: &amp;nbsp;&amp;nbsp;&amp;nbsp; ElValue = float(row.getValue(field)) &amp;nbsp;&amp;nbsp;&amp;nbsp; exp = '"{0}" = {1}'.format(field, GridValue) &amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management (GAGEMergeLayer, "NEW_SELECTION", exp) &lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;&lt;SPAN&gt;if the value is indeed a string, the safest way is to use the &lt;A href="&amp;lt;/span&amp;gt;&amp;lt;a" target="_blank"&gt;Add" rel="nofollow" target="_blank"&amp;gt;http://resources.arcgis.com/en/help/main/10.1/index.html#//018v0000006p000000]Add&lt;/A&gt;&lt;SPAN&gt; Field Delimiters&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;field = arcpy.AddFieldDelimiters(GAGELayer, "ELEVATION") cursor = arcpy.SearchCursor(GAGELayer) for row in cursor: &amp;nbsp;&amp;nbsp;&amp;nbsp; ElValue = float(row.getValue(field)) &amp;nbsp;&amp;nbsp;&amp;nbsp; exp = """ {0} = '{1}' """.format(field, GridValue) &amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management (GAGEMergeLayer, "NEW_SELECTION", exp) &lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 07 Jun 2013 16:18:30 GMT</pubDate>
    <dc:creator>Anonymous User</dc:creator>
    <dc:date>2013-06-07T16:18:30Z</dc:date>
    <item>
      <title>Help with Expression Syntax with two variables - SelectLayerByAttribute.</title>
      <link>https://community.esri.com/t5/python-questions/help-with-expression-syntax-with-two-variables/m-p/549132#M42880</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;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.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Needless to say, I'm having difficulty with the expression--I don???t have a firm grasp on the whole "\""&amp;nbsp; thing.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Here's what I have at the moment.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;field = "ELEVATION" cursor = arcpy.SearchCursor(GAGELayer) for row in cursor: &amp;nbsp;&amp;nbsp;&amp;nbsp; ElValue = float(row.getValue(field)) &amp;nbsp;&amp;nbsp;&amp;nbsp; exp = "\"" + field + "\" = " + GridValue + ""\" &amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management (GAGEMergeLayer, "NEW_SELECTION", exp)&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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?)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Something like this&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;exp = "\"" + field + "\" = 3"&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I'm not sure how to take that and make the expression work with '3' as a variable.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Any help (and a little explanation) would be greatly appreciated.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 07 Jun 2013 16:08:03 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/help-with-expression-syntax-with-two-variables/m-p/549132#M42880</guid>
      <dc:creator>JohnLay</dc:creator>
      <dc:date>2013-06-07T16:08:03Z</dc:date>
    </item>
    <item>
      <title>Re: Help with Expression Syntax with two variables - SelectLayerByAttribute.</title>
      <link>https://community.esri.com/t5/python-questions/help-with-expression-syntax-with-two-variables/m-p/549133#M42881</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Is your GridValue a string?&amp;nbsp; You only need to put string values inside the single quotes.&amp;nbsp; If it is a number you do not need to put it inside the single quotes.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;So if it is a string it would be:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;"ELEVATION" = '3'&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;If an integer:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;"ELEVATION" = 3&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;As for all the slashes and such those are for escaping.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;You can use the backslashes to escape single quotes like &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;exp = ' "Elevation" = \'3\' '&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Or you can use the triple quote method&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;exp = """ "Elevation" = '3' """&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;field = "ELEVATION" cursor = arcpy.SearchCursor(GAGELayer) for row in cursor: &amp;nbsp;&amp;nbsp;&amp;nbsp; ElValue = float(row.getValue(field)) &amp;nbsp;&amp;nbsp;&amp;nbsp; exp = '"{0}" = {1}'.format(field, GridValue) &amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management (GAGEMergeLayer, "NEW_SELECTION", exp) &lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;&lt;SPAN&gt;if the value is indeed a string, the safest way is to use the &lt;A href="&amp;lt;/span&amp;gt;&amp;lt;a" target="_blank"&gt;Add" rel="nofollow" target="_blank"&amp;gt;http://resources.arcgis.com/en/help/main/10.1/index.html#//018v0000006p000000]Add&lt;/A&gt;&lt;SPAN&gt; Field Delimiters&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;field = arcpy.AddFieldDelimiters(GAGELayer, "ELEVATION") cursor = arcpy.SearchCursor(GAGELayer) for row in cursor: &amp;nbsp;&amp;nbsp;&amp;nbsp; ElValue = float(row.getValue(field)) &amp;nbsp;&amp;nbsp;&amp;nbsp; exp = """ {0} = '{1}' """.format(field, GridValue) &amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management (GAGEMergeLayer, "NEW_SELECTION", exp) &lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 07 Jun 2013 16:18:30 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/help-with-expression-syntax-with-two-variables/m-p/549133#M42881</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2013-06-07T16:18:30Z</dc:date>
    </item>
    <item>
      <title>Re: Help with Expression Syntax with two variables - SelectLayerByAttribute.</title>
      <link>https://community.esri.com/t5/python-questions/help-with-expression-syntax-with-two-variables/m-p/549134#M42882</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Caleb,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks for you reply.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The code should actually look like this:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;field = "ELEVATION"
cursor = arcpy.SearchCursor(GAGELayer)
for row in cursor:
&amp;nbsp;&amp;nbsp;&amp;nbsp; ElValue = float(row.getValue(field))
&amp;nbsp;&amp;nbsp;&amp;nbsp; exp = "\"" + field + "\" = " + ElValue + ""\"
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management (GAGEMergeLayer, "NEW_SELECTION", exp)&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I tried&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;exp = '"{0}" = {1}'.format(field, GridValue)&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;and I'm still getting:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN style="color:#ff0000;"&gt;ExecuteError: ERROR 000358: Invalid expression&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 23:45:30 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/help-with-expression-syntax-with-two-variables/m-p/549134#M42882</guid>
      <dc:creator>JohnLay</dc:creator>
      <dc:date>2021-12-11T23:45:30Z</dc:date>
    </item>
    <item>
      <title>Re: Help with Expression Syntax with two variables - SelectLayerByAttribute.</title>
      <link>https://community.esri.com/t5/python-questions/help-with-expression-syntax-with-two-variables/m-p/549135#M42883</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Caleb,&lt;BR /&gt;&lt;BR /&gt;Thanks for you reply.&lt;BR /&gt;&lt;BR /&gt;Yes GridValue is and integer--actually it will most likely be a float value.&lt;BR /&gt;&lt;BR /&gt;The code should actually look like this:&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;field = "ELEVATION"
cursor = arcpy.SearchCursor(GAGELayer)
for row in cursor:
&amp;nbsp;&amp;nbsp;&amp;nbsp; ElValue = float(row.getValue(field))
&amp;nbsp;&amp;nbsp;&amp;nbsp; exp = "\"" + field + "\" = " + ElValue + ""\"
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management (GAGEMergeLayer, "NEW_SELECTION", exp)&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;I tried&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;exp = '"{0}" = {1}'.format(field, GridValue)&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;and I'm still getting:&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN style="color:#ff0000;"&gt;ExecuteError: ERROR 000358: Invalid expression&lt;/SPAN&gt;&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Could it be because ElValue is an integer and you are attempting to concatenate it into a string?&amp;nbsp; Maybe try changing this line:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;exp = "\"" + field + "\" = " + str(ElValue) + "\"" &lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 23:45:33 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/help-with-expression-syntax-with-two-variables/m-p/549135#M42883</guid>
      <dc:creator>JamesCrandall</dc:creator>
      <dc:date>2021-12-11T23:45:33Z</dc:date>
    </item>
    <item>
      <title>Re: Help with Expression Syntax with two variables - SelectLayerByAttribute.</title>
      <link>https://community.esri.com/t5/python-questions/help-with-expression-syntax-with-two-variables/m-p/549136#M42884</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;James is right, if ElValue is what you need to pass in (you had GridValue in the first post).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Although, I do not think all the convoluted backslashes are necessary.&amp;nbsp; The expression can simply be:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
exp = ' "ELEVATION" = %s '&amp;nbsp; %ElValue

# OR 

exp = ' "ELEVATION" =&amp;nbsp; '&amp;nbsp;&amp;nbsp; + ElValue
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Try this: &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
field = "ELEVATION"
cursor = arcpy.SearchCursor(GAGELayer)
for row in cursor:
&amp;nbsp;&amp;nbsp;&amp;nbsp; ElValue = float(row.getValue(field))
&amp;nbsp;&amp;nbsp;&amp;nbsp; exp = '"{0}" = {1}'.format(field, ElValue)
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management (GAGEMergeLayer, "NEW_SELECTION", exp) 

&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 23:45:36 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/help-with-expression-syntax-with-two-variables/m-p/549136#M42884</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2021-12-11T23:45:36Z</dc:date>
    </item>
    <item>
      <title>Re: Help with Expression Syntax with two variables - SelectLayerByAttribute.</title>
      <link>https://community.esri.com/t5/python-questions/help-with-expression-syntax-with-two-variables/m-p/549137#M42885</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Caleb,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Your suggestion&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code jive_text_macro"&gt;exp = '"{0}" = {1}'.format(field, ElValue)&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thank you as well, James.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Now off to find the rest of the bugs in this beast.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 07 Jun 2013 17:46:24 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/help-with-expression-syntax-with-two-variables/m-p/549137#M42885</guid>
      <dc:creator>JohnLay</dc:creator>
      <dc:date>2013-06-07T17:46:24Z</dc:date>
    </item>
  </channel>
</rss>

