Invalid Expression error on SelectLayerByAttribute

917
2
Jump to solution
11-13-2018 12:01 PM
CharlesGeiger
New Contributor III

My Python script uses two feature classes in a file geodatabase in ArcGIS Pro.  I select several polygons from the Parcels fc and start the script.  The script copies the polygons to the Neighborhoods fc, merges them into one polygon (using Dissolve) and should copy the script parameter entries into several fields of the new Neighborhoods polygon.  I have tried many variations on the syntax for the "where clause" in the ArcPy SelectLayerByAttribute function, but always get an Invalid Expression failure on line 41:
      arcpy.SelectLayerByAttribute_management (fc_out, "NEW_SELECTION", whereclause)

Any help would be greatly appreciated.

Here is the code:

#
# Python Script
# This script copies polygons from the Lancaster County Parcels feature
# class into the Neighborhoods feature class and processes them into a
# single neighborhood, complete with identifying attributes.

import arcpy
# Set the current workspace
from arcpy import env
env.workspace = "CURRENT"
import datetime
# Read the input parameter values:
# 0: Neighborhood Name: NbhdName
# 1: Plan Number part 1: PlanNum1
# 2: Plan Number part 2: PlanNum2
# 3: Plan Density: PlanDensity
# 4: Plan Date: PlanDate
nbhdName = arcpy.GetParameterAsText(0)
planNum1 = arcpy.GetParameterAsText(1)
planNum2 = arcpy.GetParameterAsText(2)
planNum = planNum1[0] + '-' + planNum1[-3:] + '-' + planNum2[-3:]
planDensity = float(arcpy.GetParameterAsText(3))
temp = arcpy.GetParameterAsText(4)
tempList = temp.split('/')
planDate = datetime.date(int(tempList[2]),int(tempList[0]),int(tempList[1]))
planYear = int(temp[-4:]) # extract the year from the date

# Copy the currently selected features in the parcels layer
fc_in = "Parcels_2016"
fc_tempin = "in_memory/Neighborhoods_in"
fc_tempout = "in_memory/Neighborhoods_out"
fc_out = "Neighborhoods_2016"
arcpy.CopyFeatures_management(fc_in, fc_tempin)

# Merge the copied parcels before adding to Neighborhoods fc
arcpy.Dissolve_management(fc_tempin, fc_tempout)
schema_type = "NO_TEST"
arcpy.Append_management(fc_tempout, fc_out, schema_type)

arcpy.SelectLayerByAttribute_management (fc_in, "CLEAR_SELECTION")
whereclause = '"Neighborhoods_2016.NBHD_NAME" = "NEW NBHD"'
arcpy.SelectLayerByAttribute_management (fc_out, "NEW_SELECTION", whereclause)

# result = arcpy.GetCount_management(fc_out)

fields = ["NBHD_NAME", "SUBPLAN", "DENSITY_Plan", "SubPlanDate", "SubPlanYear"]
# Create update cursor for feature class
with arcpy.da.UpdateCursor(fc_out, fields) as cursor:
for row in cursor: # DOES NOT execute on only the new neighborhood
row[0] = nbhdName.Title()
row[1] = planNum
row[2] = planDensity
row[3] = planDate
row[4] = planYear
cursor.updateRow(row)
#
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JoshuaBixby
MVP Esteemed Contributor

I am not sure if this is your only problem, but you need to wrap strings in single quotes, it is covered in the Building a query expression—Help | ArcGIS Desktop  documentation.  Try:

whereclause = "Neighborhoods_2016.NBHD_NAME = 'NEW NBHD'"

View solution in original post

2 Replies
JoshuaBixby
MVP Esteemed Contributor

I am not sure if this is your only problem, but you need to wrap strings in single quotes, it is covered in the Building a query expression—Help | ArcGIS Desktop  documentation.  Try:

whereclause = "Neighborhoods_2016.NBHD_NAME = 'NEW NBHD'"
CharlesGeiger
New Contributor III


Thank you, you got it in one.  I had to change one other bit of code:
     row[0] = nbhdName.Title()

became

      row[0] = nbhdName.title()

Everything works now.  Thank you!

0 Kudos