Select to view content in your preferred language

Invalid Expression Error in Loop

488
2
Jump to solution
04-21-2014 04:34 AM
JohnLay
Frequent Contributor
I'm extremely confused. I set up a loop to select features by attribute from a list of feature classes. The script runs completely through the list doing exactly what it's supposed to do. But then I receive and invalid expression error.

Messages:
Selecting by COFIPS
Selecting S_POL_AR
Selecting S_COMMUNITY_GROUP
Selecting V_P_FLD_HAZ_AR
Selecting V_P_FLD_HAZ_LN
Selecting V_P_HYDRAMODEL
Failed script FRISGeodatabasev12...
Traceback (most recent call last):
File "\\Ncemjfhqfs01\gtm\GIS\GTM_GIS_Tools\Scripts\FRISGeodatabase_v1.2.py", line 260, in <module>
arcpy.SelectLayerByAttribute_management (featurelayer, "NEW_SELECTION", CLAUSE)
File "c:\program files (x86)\arcgis\desktop10.2\arcpy\arcpy\management.py", line 6688, in SelectLayerByAttribute
raise e
ExecuteError: ERROR 000358: Invalid expression
Failed to execute (SelectLayerByAttribute).

The cod is as follows:
PrelimSelectByCOFIPS = ["Database Connections\NC_FLOOD.sde\NC_FLOOD.DBO.BASE\NC_FLOOD.DBO.S_POL_AR", "Database Connections\NC_FLOOD.sde\NC_FLOOD.DBO.BASE\NC_FLOOD.DBO.S_COMMUNITY_GROUP", "Database Connections\NC_FLOOD.sde\NC_FLOOD.DBO.FLOOD\NC_FLOOD.DBO.V_P_FLD_HAZ_AR", "Database Connections\NC_FLOOD.sde\NC_FLOOD.DBO.FLOOD\NC_FLOOD.DBO.V_P_FLD_HAZ_LN", "Database Connections\NC_FLOOD.sde\NC_FLOOD.DBO.FLOOD\NC_FLOOD.DBO.V_P_HYDRAMODEL"]

# Select Features by COFIPS
arcpy.AddMessage("Selecting by COFIPS")
CLAUSE = ('{0} = {1}'.format ("CO_FIPS", COFIPS))
for feature in PrelimSelectByCOFIPS:
    outfeaturename = string.join(feature.rstrip().split('.')[-1:],'.')
    arcpy.AddMessage("Selecting " + outfeaturename)
    featurelayer = os.path.join(FRIS_FGDB, outfeaturename + "_layer")
    COFIPSfeature = os.path.join(FRIS_FGDB, outfeaturename)
    arcpy.MakeFeatureLayer_management(feature, featurelayer)
    arcpy.SelectLayerByAttribute_management (featurelayer, "NEW_SELECTION", CLAUSE)
    arcpy.CopyFeatures_management(featurelayer, COFIPSfeature)
    arcpy.Delete_management(featurelayer)


I have the exact same loop set up two other times before this in the script to select by location and to clip by selected feature and neither of those give me any issue.

What is going on here?
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
T__WayneWhitley
Honored Contributor
Seems to be having a problem with your expression - maybe you have some null values?:

CLAUSE = ('{0} = {1}'.format ("CO_FIPS", COFIPS))

If the problem is with the value of your variable COFIPS, then check that...otherwise, if for some reason your problem is with the fieldname "CO_FIPS" then you can try making sure the field exists and perhaps use the addfielddelimiters method.

If the script had problems with one of the other parameters, you'd likely get a msg similar to "INVALID PARAMETERS".


Wayne



PS- By the way, there's a sample using the AddFieldDelimiters function here that appears to contain an error:

AddFieldDelimiters (arcpy)
http://resources.arcgis.com/en/help/main/10.2/index.html#//018v0000006p000000
# This appears to be in error- 'field_name' is used twice in the expression: sql_exp = """{0} = {1}""".format(     arcpy.AddFieldDelimiters('c:/data', field_name),     field_name)  # ...should likely be 'state_value', the only unused parameter. # Also, note this would only work for numerical fields; txt fields would be: #  """{0} = '{1}'""" so on and so forth... sql_exp = """{0} = {1}""".format(     arcpy.AddFieldDelimiters('c:/data', field_name),     state_value)

View solution in original post

0 Kudos
2 Replies
T__WayneWhitley
Honored Contributor
Seems to be having a problem with your expression - maybe you have some null values?:

CLAUSE = ('{0} = {1}'.format ("CO_FIPS", COFIPS))

If the problem is with the value of your variable COFIPS, then check that...otherwise, if for some reason your problem is with the fieldname "CO_FIPS" then you can try making sure the field exists and perhaps use the addfielddelimiters method.

If the script had problems with one of the other parameters, you'd likely get a msg similar to "INVALID PARAMETERS".


Wayne



PS- By the way, there's a sample using the AddFieldDelimiters function here that appears to contain an error:

AddFieldDelimiters (arcpy)
http://resources.arcgis.com/en/help/main/10.2/index.html#//018v0000006p000000
# This appears to be in error- 'field_name' is used twice in the expression: sql_exp = """{0} = {1}""".format(     arcpy.AddFieldDelimiters('c:/data', field_name),     field_name)  # ...should likely be 'state_value', the only unused parameter. # Also, note this would only work for numerical fields; txt fields would be: #  """{0} = '{1}'""" so on and so forth... sql_exp = """{0} = {1}""".format(     arcpy.AddFieldDelimiters('c:/data', field_name),     state_value)
0 Kudos
JohnLay
Frequent Contributor
Wayne, you were correct. My error was actually that one of the feature classes did not contain the "CO_FIPS" field. I missed it because the export was already produced in an earlier process.
0 Kudos