<?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: Issue: Invalid Expression Failed to Execute (SelectLayerByAttribute) in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/issue-invalid-expression-failed-to-execute/m-p/1509514#M71114</link>
    <description>&lt;P&gt;A few potential issues.&amp;nbsp; Firstly, why all the curly brackets?&amp;nbsp; If you are trying to use Python string formatting, you are missing calling the format function itself.&amp;nbsp;&amp;nbsp; Assuming the unique_id is a number and not a string, try&lt;/P&gt;&lt;LI-CODE lang="python"&gt;query = '"{}" = {}'.format(unique_id_field, unique_id)&lt;/LI-CODE&gt;</description>
    <pubDate>Wed, 24 Jul 2024 18:48:20 GMT</pubDate>
    <dc:creator>JoshuaBixby</dc:creator>
    <dc:date>2024-07-24T18:48:20Z</dc:date>
    <item>
      <title>Issue: Invalid Expression Failed to Execute (SelectLayerByAttribute)</title>
      <link>https://community.esri.com/t5/python-questions/issue-invalid-expression-failed-to-execute/m-p/1509472#M71111</link>
      <description>&lt;P&gt;I have used ArcGIS Desktop 10.8.2, and I have an issue running the script with an error message about SQL expression in the script written in the title. I would appreciate it if you could support fixing this.&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy

# Define the input and output paths
input_shapefile = r"C:\...\..shp"
output_folder = r"C:\...\(folder)"

# Define the field name that contains unique IDs
unique_id_field = 'AreaKey'

# Create a feature layer from the input shapefile
arcpy.MakeFeatureLayer_management(input_shapefile, "temp_layer")

# Get a list of unique IDs from the shapefile
unique_ids = set([row[0] for row in arcpy.da.SearchCursor(input_shapefile, [unique_id_field])])

# Iterate through each unique ID
for unique_id in unique_ids:
    # Define the SQL query to select the feature by unique ID
    query = '"{unique_id_field}" = {unique_id}'

    # Select the feature using the query
    arcpy.SelectLayerByAttribute_management("temp_layer", "NEW_SELECTION", query)

    # Check if the selection contains any features
    selected_count = int(arcpy.GetCount_management("temp_layer").getOutput(0))
    if selected_count &amp;gt; 0:
        # Define the output shapefile path
        output_shapefile = "{output_folder}\\{unique_id}.shp"

        # Export the selected feature(s) to a new shapefile
        arcpy.CopyFeatures_management("temp_layer", output_shapefile)
        print("Successfully exported {selected_count} feature(s) with ID {unique_id} to {output_shapefile}")
    else:
        print("No features found with the unique ID {unique_id}.")

# Clean up the temporary layer
arcpy.Delete_management("temp_layer")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 24 Jul 2024 17:59:29 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/issue-invalid-expression-failed-to-execute/m-p/1509472#M71111</guid>
      <dc:creator>KioshiMishiro</dc:creator>
      <dc:date>2024-07-24T17:59:29Z</dc:date>
    </item>
    <item>
      <title>Re: Issue: Invalid Expression Failed to Execute (SelectLayerByAttribute)</title>
      <link>https://community.esri.com/t5/python-questions/issue-invalid-expression-failed-to-execute/m-p/1509484#M71112</link>
      <description>&lt;P&gt;Hey &lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/299042"&gt;@KioshiMishiro&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Looking into this now, but I do see something that could cause an issue. In your where clause, attempt to swap the double quotes with single quotes, like this:&lt;/P&gt;&lt;LI-CODE lang="c"&gt;query = "'{unique_id_field}' = {unique_id}"&lt;/LI-CODE&gt;&lt;P&gt;If this works, let me know!&lt;/P&gt;&lt;P&gt;Cody&lt;/P&gt;</description>
      <pubDate>Wed, 24 Jul 2024 18:16:38 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/issue-invalid-expression-failed-to-execute/m-p/1509484#M71112</guid>
      <dc:creator>CodyPatterson</dc:creator>
      <dc:date>2024-07-24T18:16:38Z</dc:date>
    </item>
    <item>
      <title>Re: Issue: Invalid Expression Failed to Execute (SelectLayerByAttribute)</title>
      <link>https://community.esri.com/t5/python-questions/issue-invalid-expression-failed-to-execute/m-p/1509489#M71113</link>
      <description>&lt;P&gt;Thank you very much&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/712076"&gt;@CodyPatterson&lt;/a&gt;, but still have same issue like below:&lt;/P&gt;&lt;TABLE border="1" width="100%"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD width="100%"&gt;&lt;P&gt;[...]&lt;/P&gt;&lt;P&gt;Executing: SelectLayerByAttribute temp_layer NEW_SELECTION "'{unique_id_field}' = {unique_id}"&lt;BR /&gt;Start Time: Wed Jul 24 13:17:24 2024&lt;BR /&gt;ERROR 000358: Invalid expression&lt;BR /&gt;An invalid SQL statement was used.&lt;BR /&gt;An invalid SQL statement was used.&lt;BR /&gt;Failed to execute (SelectLayerByAttribute).&lt;BR /&gt;Failed at Wed Jul 24 13:17:24 2024 (Elapsed Time: 0.03 seconds) [...]&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 24 Jul 2024 18:24:44 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/issue-invalid-expression-failed-to-execute/m-p/1509489#M71113</guid>
      <dc:creator>KioshiMishiro</dc:creator>
      <dc:date>2024-07-24T18:24:44Z</dc:date>
    </item>
    <item>
      <title>Re: Issue: Invalid Expression Failed to Execute (SelectLayerByAttribute)</title>
      <link>https://community.esri.com/t5/python-questions/issue-invalid-expression-failed-to-execute/m-p/1509514#M71114</link>
      <description>&lt;P&gt;A few potential issues.&amp;nbsp; Firstly, why all the curly brackets?&amp;nbsp; If you are trying to use Python string formatting, you are missing calling the format function itself.&amp;nbsp;&amp;nbsp; Assuming the unique_id is a number and not a string, try&lt;/P&gt;&lt;LI-CODE lang="python"&gt;query = '"{}" = {}'.format(unique_id_field, unique_id)&lt;/LI-CODE&gt;</description>
      <pubDate>Wed, 24 Jul 2024 18:48:20 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/issue-invalid-expression-failed-to-execute/m-p/1509514#M71114</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2024-07-24T18:48:20Z</dc:date>
    </item>
    <item>
      <title>Re: Issue: Invalid Expression Failed to Execute (SelectLayerByAttribute)</title>
      <link>https://community.esri.com/t5/python-questions/issue-invalid-expression-failed-to-execute/m-p/1509526#M71115</link>
      <description>&lt;P&gt;That is not a number, but it is a string.&lt;/P&gt;</description>
      <pubDate>Wed, 24 Jul 2024 19:02:36 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/issue-invalid-expression-failed-to-execute/m-p/1509526#M71115</guid>
      <dc:creator>KioshiMishiro</dc:creator>
      <dc:date>2024-07-24T19:02:36Z</dc:date>
    </item>
    <item>
      <title>Re: Issue: Invalid Expression Failed to Execute (SelectLayerByAttribute)</title>
      <link>https://community.esri.com/t5/python-questions/issue-invalid-expression-failed-to-execute/m-p/1509575#M71116</link>
      <description>&lt;P&gt;In Python 3, the f string syntax is actaully preferred now:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;query = f"'{unique_id_field}' = {unique_id}"&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;they make it easier to keep track of what is being inserted where because your eyes don't have to keep darting back and forth.&lt;/P&gt;</description>
      <pubDate>Wed, 24 Jul 2024 20:26:13 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/issue-invalid-expression-failed-to-execute/m-p/1509575#M71116</guid>
      <dc:creator>HaydenWelch</dc:creator>
      <dc:date>2024-07-24T20:26:13Z</dc:date>
    </item>
    <item>
      <title>Re: Issue: Invalid Expression Failed to Execute (SelectLayerByAttribute)</title>
      <link>https://community.esri.com/t5/python-questions/issue-invalid-expression-failed-to-execute/m-p/1509589#M71117</link>
      <description>&lt;P&gt;Unfortunately, the OP is using ArcMap 10.8.2, which is Python 2.x.&lt;/P&gt;</description>
      <pubDate>Wed, 24 Jul 2024 20:36:26 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/issue-invalid-expression-failed-to-execute/m-p/1509589#M71117</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2024-07-24T20:36:26Z</dc:date>
    </item>
    <item>
      <title>Re: Issue: Invalid Expression Failed to Execute (SelectLayerByAttribute)</title>
      <link>https://community.esri.com/t5/python-questions/issue-invalid-expression-failed-to-execute/m-p/1509608#M71118</link>
      <description>&lt;P&gt;Always forget that Arcmap is still using Python 2...&lt;/P&gt;</description>
      <pubDate>Wed, 24 Jul 2024 20:57:37 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/issue-invalid-expression-failed-to-execute/m-p/1509608#M71118</guid>
      <dc:creator>HaydenWelch</dc:creator>
      <dc:date>2024-07-24T20:57:37Z</dc:date>
    </item>
    <item>
      <title>Re: Issue: Invalid Expression Failed to Execute (SelectLayerByAttribute)</title>
      <link>https://community.esri.com/t5/python-questions/issue-invalid-expression-failed-to-execute/m-p/1509623#M71120</link>
      <description>&lt;P&gt;Strings must be single-quoted:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;query = '"{}" = \'{}\''.format(unique_id_field, unique_id)&lt;/LI-CODE&gt;</description>
      <pubDate>Wed, 24 Jul 2024 21:13:05 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/issue-invalid-expression-failed-to-execute/m-p/1509623#M71120</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2024-07-24T21:13:05Z</dc:date>
    </item>
    <item>
      <title>Re: Issue: Invalid Expression Failed to Execute (SelectLayerByAttribute)</title>
      <link>https://community.esri.com/t5/python-questions/issue-invalid-expression-failed-to-execute/m-p/1510192#M71121</link>
      <description>&lt;P&gt;Thank you all. I solved the issues based on your comments. Really appreciate.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 25 Jul 2024 18:19:21 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/issue-invalid-expression-failed-to-execute/m-p/1510192#M71121</guid>
      <dc:creator>KioshiMishiro</dc:creator>
      <dc:date>2024-07-25T18:19:21Z</dc:date>
    </item>
  </channel>
</rss>

