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.
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 > 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")