<?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: Variables in Select by Attribute Where Clause in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/variables-in-select-by-attribute-where-clause/m-p/1324974#M68521</link>
    <description>&lt;P&gt;I just tried that again, just to make extra sure I wasn't crazy (sort of hoped I was).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;where_clause = "{} = {}".format(repl_field,value)
arcpy.management.SelectLayerByAttribute(grid,"NEW_SELECTION",
                                        where_clause)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;And it still didn't work. I don't know why--maybe something in my install is broken, or I just need to restart my computer.&lt;/P&gt;</description>
    <pubDate>Fri, 01 Sep 2023 21:41:03 GMT</pubDate>
    <dc:creator>andrewcollins12</dc:creator>
    <dc:date>2023-09-01T21:41:03Z</dc:date>
    <item>
      <title>Variables in Select by Attribute Where Clause</title>
      <link>https://community.esri.com/t5/python-questions/variables-in-select-by-attribute-where-clause/m-p/1324936#M68514</link>
      <description>&lt;P&gt;Hello!&lt;/P&gt;&lt;P&gt;I'm having a heck of a time (read: I'm about to throw my computer down the stairs) trying to use two variables (both field and value) in a Select by Attribute query. I'm working in ArcGIS Pro 3.1/Python 3.9.&lt;/P&gt;&lt;P&gt;Here's the use case: I want to select points with a given value in a given field from a large grid of points in a point shapefile, and then export those points to a new shapefile. Most recent code attempt below.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy
import os

# Input point shapefile, very large
grid = arcpy.GetParameterAsText(0) 

# Specific area where I want to extract points with certain value
aoi = arcpy.GetParameterAsText(1) 

# Field in grid to use for values to query (dtype = double)
repl_field = arcpy.GetParameterAsText(2) 

# User-specified value to match with points to export (dtype = double)
value = arcpy.GetParameter(3) 

# Scratch workspace for intermediate data/debugging
ws = arcpy.GetParameterAsText(4) 

# Intermediate files
int_grid = os.path.join(ws, "int_grid.shp")
final_grid = os.path.join(ws, "final_grid.shp")

# Step 1: extract just the points inside AOI; this works as expected
arcpy.management.SelectLayerByLocation(grid,"COMPLETELY_WITHIN",aoi,"","NEW_SELECTION")
arcpy.conversion.ExportFeatures(grid,int_grid)

# Step 2: this selection isn't working; all points are exported, 
# not just those that match value.
where_clause = '"{}" = {}'.format(repl_field,value)
arcpy.management.SelectLayerByAttribute(int_grid,"NEW_SELECTION",where_clause)
arcpy.conversion.ExportFeatures(int_grid,final_grid)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've also tried the methods commented &lt;A href="https://community.esri.com/t5/python-questions/select-by-attribute-with-variable/m-p/1053641#M60982" target="_self"&gt;here&lt;/A&gt;, &lt;A href="https://community.esri.com/t5/geoprocessing-questions/using-a-variable-in-sql-to-select-by-attribute/td-p/749764" target="_self"&gt;here&lt;/A&gt;, and &lt;A href="https://gis.stackexchange.com/questions/115989/selecting-layer-by-attribute-using-sql-and-variable-in-arcpy" target="_self"&gt;here&lt;/A&gt;&amp;nbsp;(basically all just different ways of formatting the where clause), with no luck. No matter how I format it, I just can't get it to select and export the correct data.&lt;/P&gt;&lt;P&gt;When I enter this code (and several other versions) line-by-line in the Python console in ArcGIS Pro, it works perfectly. But I need it to run in this script. I'm thoroughly at a loss and would be very appreciative of any suggestions.&lt;/P&gt;</description>
      <pubDate>Fri, 01 Sep 2023 20:43:29 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/variables-in-select-by-attribute-where-clause/m-p/1324936#M68514</guid>
      <dc:creator>andrewcollins12</dc:creator>
      <dc:date>2023-09-01T20:43:29Z</dc:date>
    </item>
    <item>
      <title>Re: Variables in Select by Attribute Where Clause</title>
      <link>https://community.esri.com/t5/python-questions/variables-in-select-by-attribute-where-clause/m-p/1324941#M68515</link>
      <description>&lt;P&gt;I'm not sure the FieldName needs to be in quotes, to be honest.&lt;/P&gt;</description>
      <pubDate>Fri, 01 Sep 2023 20:25:53 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/variables-in-select-by-attribute-where-clause/m-p/1324941#M68515</guid>
      <dc:creator>AlfredBaldenweck</dc:creator>
      <dc:date>2023-09-01T20:25:53Z</dc:date>
    </item>
    <item>
      <title>Re: Variables in Select by Attribute Where Clause</title>
      <link>https://community.esri.com/t5/python-questions/variables-in-select-by-attribute-where-clause/m-p/1324947#M68516</link>
      <description>&lt;P&gt;Thanks &lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/458875"&gt;@AlfredBaldenweck&lt;/a&gt;! I tried it without the quotes around the field name as well, unfortunately the result is the same.&lt;/P&gt;&lt;P&gt;I've also tried:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;where_clause = '{} = {}'.format(repl_field,value) # all points export

where_clause = '\"{}\" ='.format(repl_field) + str(value) # invalid expression error

where_clause = '\"{}\" ='.format(repl_field) + value # invalid expression error

field_name = arcpy.AddFieldDelimiters(int_grid,repl_field)
where_clause = f'{field_name} = {value}' # all points export

# and just doing it inline, as below, but all points export
arcpy.management.SelectLayerByAttribute(int_grid,"NEW_SELECTION",
                                        '"{}" = {}'.format(repl_field,value))

arcpy.management.SelectLayerByAttribute(int_grid,"NEW_SELECTION",
                                        '{} = {}'.format(repl_field,value))&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 01 Sep 2023 20:40:46 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/variables-in-select-by-attribute-where-clause/m-p/1324947#M68516</guid>
      <dc:creator>andrewcollins12</dc:creator>
      <dc:date>2023-09-01T20:40:46Z</dc:date>
    </item>
    <item>
      <title>Re: Variables in Select by Attribute Where Clause</title>
      <link>https://community.esri.com/t5/python-questions/variables-in-select-by-attribute-where-clause/m-p/1324955#M68517</link>
      <description>&lt;P&gt;Apologies, I'm in 2.9 still so I had to change it a bit (Export Features) and I didn't use shapefiles, but this worked for me. The other thing is I assumed you're feeding it file paths, and made sure to assign the selections as variables.&lt;/P&gt;&lt;P&gt;I think the issue is actually that you have the quotes in the wrong spot. SQL uses single quotes for strings, and right now you're feeding it a string without quotes, so it thinks you're looking for a field or something.&lt;/P&gt;&lt;LI-CODE lang="c"&gt;import arcpy
import os
# Input point shapefile, very large
grid = arcpy.GetParameterAsText(0) 
# Specific area where I want to extract points with certain value
aoi = arcpy.GetParameterAsText(1) 
# Field in grid to use for values to query (dtype = double)
repl_field = arcpy.GetParameterAsText(2) 
# User-specified value to match with points to export (dtype = double)
value = arcpy.GetParameter(3) 
# Scratch workspace for intermediate data/debugging
ws = arcpy.GetParameterAsText(4) 
# Intermediate files
int_grid = os.path.join(ws, 'int_grid')
final_grid = os.path.join(ws, 'final_grid')
# Step 1: extract just the points inside AOI; this works as expected
grid = arcpy.management.SelectLayerByLocation(grid,"COMPLETELY_WITHIN",aoi,"","NEW_SELECTION")
arcpy.conversion.FeatureClassToFeatureClass(grid,ws,'int_grid')
# Step 2: this selection isn't working; all points are exported, 
# not just those that match value.
where_clause = "{} = '{}'".format(repl_field,value)
#arcpy.AddMessage(where_clause)
int_grid = arcpy.management.SelectLayerByAttribute(int_grid,"NEW_SELECTION",where_clause)
arcpy.conversion.FeatureClassToFeatureClass(int_grid,ws,'final_grid')&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This will work so long as you're looking for stuff in a text field; if you feed it a number it will return something empty or break, I expect.&lt;/P&gt;&lt;P&gt;Hope this helps!&lt;/P&gt;</description>
      <pubDate>Fri, 01 Sep 2023 20:57:42 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/variables-in-select-by-attribute-where-clause/m-p/1324955#M68517</guid>
      <dc:creator>AlfredBaldenweck</dc:creator>
      <dc:date>2023-09-01T20:57:42Z</dc:date>
    </item>
    <item>
      <title>Re: Variables in Select by Attribute Where Clause</title>
      <link>https://community.esri.com/t5/python-questions/variables-in-select-by-attribute-where-clause/m-p/1324957#M68518</link>
      <description>&lt;P&gt;The other thing is that you might have a more efficient time if you chain the two selects together, rather than exporting each one.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;final_grid = os.path.join(ws, 'final_grid')
where_clause = "{} = '{}'".format(repl_field,value)
grid = arcpy.management.SelectLayerByAttribute(grid,"NEW_SELECTION",where_clause)
grid = arcpy.management.SelectLayerByLocation(grid,"COMPLETELY_WITHIN",aoi,"","NEW_SELECTION")
arcpy.conversion.FeatureClassToFeatureClass(grid,ws,'final_grid')&lt;/LI-CODE&gt;</description>
      <pubDate>Fri, 01 Sep 2023 21:07:43 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/variables-in-select-by-attribute-where-clause/m-p/1324957#M68518</guid>
      <dc:creator>AlfredBaldenweck</dc:creator>
      <dc:date>2023-09-01T21:07:43Z</dc:date>
    </item>
    <item>
      <title>Re: Variables in Select by Attribute Where Clause</title>
      <link>https://community.esri.com/t5/python-questions/variables-in-select-by-attribute-where-clause/m-p/1324961#M68519</link>
      <description>&lt;P&gt;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/458875"&gt;@AlfredBaldenweck&lt;/a&gt;&amp;nbsp;unfortunately it is a number that I'm trying to match--both the field and the user-input value are dtype=double, so I didn't think that would be an issue. As you predicted, it broke when I tried to put the number in quotes, which makes sense. I tried changing the user-input value to string, but that didn't work either (predictably).&lt;/P&gt;&lt;P&gt;Unfortunately I can't really mark your answer as a solution; HOWEVER your comment did give me an idea, which was to isolate whether it was a syntax issue or a tool-related issue. So I used the where_clause from my original post with the ExportFeatures tool instead of the selection tool, and it worked! So, for that idea, many thanks!&lt;/P&gt;&lt;LI-CODE lang="python"&gt;# This worked!
where_clause = '"{}" = {}'.format(repl_field,value)
arcpy.conversion.ExportFeatures(grid,final_grid,where_clause)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I can't really mark this as a solution either, sadly, since it's just a workaround&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":face_with_tears_of_joy:"&gt;😂&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Moral of the story: seems to be an issue with SelectLayerByAttribute.&lt;/P&gt;</description>
      <pubDate>Fri, 01 Sep 2023 21:19:26 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/variables-in-select-by-attribute-where-clause/m-p/1324961#M68519</guid>
      <dc:creator>andrewcollins12</dc:creator>
      <dc:date>2023-09-01T21:19:26Z</dc:date>
    </item>
    <item>
      <title>Re: Variables in Select by Attribute Where Clause</title>
      <link>https://community.esri.com/t5/python-questions/variables-in-select-by-attribute-where-clause/m-p/1324966#M68520</link>
      <description>&lt;P&gt;Weird.&lt;/P&gt;&lt;P&gt;I just tested again, this time on a double field (Completely missed that the first time), and the same except for removing those single quotes, and it worked fine for me.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 01 Sep 2023 21:31:20 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/variables-in-select-by-attribute-where-clause/m-p/1324966#M68520</guid>
      <dc:creator>AlfredBaldenweck</dc:creator>
      <dc:date>2023-09-01T21:31:20Z</dc:date>
    </item>
    <item>
      <title>Re: Variables in Select by Attribute Where Clause</title>
      <link>https://community.esri.com/t5/python-questions/variables-in-select-by-attribute-where-clause/m-p/1324974#M68521</link>
      <description>&lt;P&gt;I just tried that again, just to make extra sure I wasn't crazy (sort of hoped I was).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;where_clause = "{} = {}".format(repl_field,value)
arcpy.management.SelectLayerByAttribute(grid,"NEW_SELECTION",
                                        where_clause)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;And it still didn't work. I don't know why--maybe something in my install is broken, or I just need to restart my computer.&lt;/P&gt;</description>
      <pubDate>Fri, 01 Sep 2023 21:41:03 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/variables-in-select-by-attribute-where-clause/m-p/1324974#M68521</guid>
      <dc:creator>andrewcollins12</dc:creator>
      <dc:date>2023-09-01T21:41:03Z</dc:date>
    </item>
    <item>
      <title>Re: Variables in Select by Attribute Where Clause</title>
      <link>https://community.esri.com/t5/python-questions/variables-in-select-by-attribute-where-clause/m-p/1324981#M68522</link>
      <description>&lt;P&gt;I know earlier you mentioned that it exported all the points when you tried that; I think it's because you aren't assigning it as a variable; you're telling it to select from the table but not telling it what to do with that selection.&lt;/P&gt;&lt;P&gt;Of course, if it's breaking (throwing an error) that's a different story, but if all it's doing is ignoring the selection, that's probably why.&lt;/P&gt;</description>
      <pubDate>Fri, 01 Sep 2023 21:44:19 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/variables-in-select-by-attribute-where-clause/m-p/1324981#M68522</guid>
      <dc:creator>AlfredBaldenweck</dc:creator>
      <dc:date>2023-09-01T21:44:19Z</dc:date>
    </item>
    <item>
      <title>Re: Variables in Select by Attribute Where Clause</title>
      <link>https://community.esri.com/t5/python-questions/variables-in-select-by-attribute-where-clause/m-p/1325008#M68523</link>
      <description>&lt;P&gt;Well I'll be darned. I've been using ArcPy for over a decade at this point, and I didn't know you could assign a selection to a variable. Embarrassing. But you're absolutely right.&lt;/P&gt;</description>
      <pubDate>Fri, 01 Sep 2023 22:37:27 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/variables-in-select-by-attribute-where-clause/m-p/1325008#M68523</guid>
      <dc:creator>andrewcollins12</dc:creator>
      <dc:date>2023-09-01T22:37:27Z</dc:date>
    </item>
    <item>
      <title>Re: Variables in Select by Attribute Where Clause</title>
      <link>https://community.esri.com/t5/python-questions/variables-in-select-by-attribute-where-clause/m-p/1354786#M69312</link>
      <description>&lt;P&gt;You mentioned that the script works if you run them line by line in the python window, but did not work as a script.&lt;/P&gt;&lt;P&gt;I was wondering whether you ran the script in the python window or as a standalone script outside of ArcGIS Pro?&lt;/P&gt;</description>
      <pubDate>Wed, 29 Nov 2023 19:03:42 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/variables-in-select-by-attribute-where-clause/m-p/1354786#M69312</guid>
      <dc:creator>JingchaoZhou</dc:creator>
      <dc:date>2023-11-29T19:03:42Z</dc:date>
    </item>
    <item>
      <title>Re: Variables in Select by Attribute Where Clause</title>
      <link>https://community.esri.com/t5/python-questions/variables-in-select-by-attribute-where-clause/m-p/1374146#M69713</link>
      <description>&lt;P&gt;Both. When I ran the code in the original post line-by-line in the python window within ArcGIS Pro, it all ran correctly. But something was breaking with the select functions when I ran it as a standalone script outside of Arc Pro. The solution proposed by&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/458875"&gt;@AlfredBaldenweck&lt;/a&gt;--to assign the selection to a variable--enabled me to run it as a standalone script.&lt;/P&gt;</description>
      <pubDate>Wed, 24 Jan 2024 21:05:02 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/variables-in-select-by-attribute-where-clause/m-p/1374146#M69713</guid>
      <dc:creator>andrewcollins12</dc:creator>
      <dc:date>2024-01-24T21:05:02Z</dc:date>
    </item>
  </channel>
</rss>

