<?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: Select by attribute using a variable in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/select-by-attribute-using-a-variable/m-p/642273#M50074</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;For future reference, "doesn't work" is not useful and makes it difficult for people to answer your question. What was the problem exactly - i.e. did you get an error message, did your script crash, what line did it crash on, did python crash with some sort of windows error, did your computer reboot, did smoke come out of your computer vents, etc...? &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Luckily, I think I can see your problem. MX is a number, you can't add a number to a string, you'll get a TypeError. You can use the following instead:&lt;/SPAN&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;"CHARTSCALE = '" + str(MX) + "'"
#or
"CHARTSCALE = '%s'"%MX&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sun, 12 Dec 2021 03:17:42 GMT</pubDate>
    <dc:creator>Luke_Pinner</dc:creator>
    <dc:date>2021-12-12T03:17:42Z</dc:date>
    <item>
      <title>Select by attribute using a variable</title>
      <link>https://community.esri.com/t5/python-questions/select-by-attribute-using-a-variable/m-p/642272#M50073</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I have created the following script to select the biggest extent in the mapview using an select by attribute tool which must contain an expression for where clause. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I have also followed other posts like Kirsten 's post who has tried to solve exactly the same problem. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;My problem is that the expression doesn't work. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;

 # Import modules
import sys,os,math,string,arcpy
from arcpy import env

# Read from current map
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd, "Data Themes")[0]
lyr = arcpy.mapping.ListLayers(mxd, "CHS_Chart_Extents", df)[0]
#The DataFrame extent object is converted into a polygon feature so it can be used with the SelectLayerByLocation function.
dfAsFeature = arcpy.Polygon(arcpy.Array([df.extent.lowerLeft, df.extent.lowerRight, df.extent.upperRight, df.extent.upperLeft]),
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; df.spatialReference)
# Select the extents into the df map view
arcpy.SelectLayerByLocation_management(lyr, "WITHIN", dfAsFeature, "", "NEW_SELECTION")


 # Create the search cursor
#
cur = arcpy.SearchCursor(lyr)
# This list will keep the numbers
featureList = []
#This loop will go through rows to find chart Numbers 
for row in cur:

&amp;nbsp;&amp;nbsp; featureList.append(row.CHARTSCALE)

# This is the maximum scale in mapview
MX = max(featureList)
# this message says that the script is OK so far
arcpy.AddMessage(max(featureList))

# Now I need to select only the biggest extent in the mapview with maximum extent
arcpy.SelectLayerByAttribute_management (lyr, "NEW_SELECTION",&amp;nbsp; "CHARTSCALE = '" + MX + "'")

&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;My problem is here&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;

# This is the maximum scale in mapview
MX = max(featureList)

# Now I need to select only the biggest extent in the mapview with maximum extent
arcpy.SelectLayerByAttribute_management (lyr, "NEW_SELECTION",&amp;nbsp; "CHARTSCALE = '" + MX + "'")

&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;How sould I write my where clause to work?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 08 Mar 2013 01:32:35 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/select-by-attribute-using-a-variable/m-p/642272#M50073</guid>
      <dc:creator>babakkasraei</dc:creator>
      <dc:date>2013-03-08T01:32:35Z</dc:date>
    </item>
    <item>
      <title>Re: Select by attribute using a variable</title>
      <link>https://community.esri.com/t5/python-questions/select-by-attribute-using-a-variable/m-p/642273#M50074</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;For future reference, "doesn't work" is not useful and makes it difficult for people to answer your question. What was the problem exactly - i.e. did you get an error message, did your script crash, what line did it crash on, did python crash with some sort of windows error, did your computer reboot, did smoke come out of your computer vents, etc...? &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Luckily, I think I can see your problem. MX is a number, you can't add a number to a string, you'll get a TypeError. You can use the following instead:&lt;/SPAN&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;"CHARTSCALE = '" + str(MX) + "'"
#or
"CHARTSCALE = '%s'"%MX&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 03:17:42 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/select-by-attribute-using-a-variable/m-p/642273#M50074</guid>
      <dc:creator>Luke_Pinner</dc:creator>
      <dc:date>2021-12-12T03:17:42Z</dc:date>
    </item>
    <item>
      <title>Re: Select by attribute using a variable</title>
      <link>https://community.esri.com/t5/python-questions/select-by-attribute-using-a-variable/m-p/642274#M50075</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;For future reference, "doesn't work" is not useful and makes it difficult for people to answer your question. What was the problem exactly - i.e. did you get an error message, did your script crash, what line did it crash on, did python crash with some sort of windows error, did your computer reboot, did smoke come out of your computer vents, etc...? &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;BR /&gt;&lt;BR /&gt;Luckily, I think I can see your problem. MX is a number, you can't add a number to a string, you'll get a TypeError. You can use the following instead:&lt;PRE class="lia-code-sample line-numbers language-none"&gt;"CHARTSCALE = '" + str(MX) + "'"
#or
"CHARTSCALE = '%s'"%MX&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Hi Luck &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;You are absolutely right. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I had to describe what exactly happened. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks for the codes. I will try them and thank you for your reminder. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Cheers&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Babak&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 03:17:45 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/select-by-attribute-using-a-variable/m-p/642274#M50075</guid>
      <dc:creator>babakkasraei</dc:creator>
      <dc:date>2021-12-12T03:17:45Z</dc:date>
    </item>
  </channel>
</rss>

