<?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: Invalid Expression Error in Loop in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/invalid-expression-error-in-loop/m-p/402296#M31678</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;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.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Mon, 21 Apr 2014 12:37:34 GMT</pubDate>
    <dc:creator>JohnLay</dc:creator>
    <dc:date>2014-04-21T12:37:34Z</dc:date>
    <item>
      <title>Invalid Expression Error in Loop</title>
      <link>https://community.esri.com/t5/python-questions/invalid-expression-error-in-loop/m-p/402294#M31676</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;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.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Messages:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Selecting by COFIPS&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Selecting S_POL_AR&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Selecting S_COMMUNITY_GROUP&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Selecting V_P_FLD_HAZ_AR&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Selecting V_P_FLD_HAZ_LN&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Selecting V_P_HYDRAMODEL&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Failed script FRISGeodatabasev12...&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Traceback (most recent call last):&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; File "\\Ncemjfhqfs01\gtm\GIS\GTM_GIS_Tools\Scripts\FRISGeodatabase_v1.2.py", line 260, in &amp;lt;module&amp;gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; arcpy.SelectLayerByAttribute_management (featurelayer, "NEW_SELECTION", CLAUSE)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; File "c:\program files (x86)\arcgis\desktop10.2\arcpy\arcpy\management.py", line 6688, in SelectLayerByAttribute&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; raise e&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;ExecuteError: ERROR 000358: Invalid expression&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Failed to execute (SelectLayerByAttribute).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The cod is as follows:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;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:
&amp;nbsp;&amp;nbsp;&amp;nbsp; outfeaturename = string.join(feature.rstrip().split('.')[-1:],'.')
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage("Selecting " + outfeaturename)
&amp;nbsp;&amp;nbsp;&amp;nbsp; featurelayer = os.path.join(FRIS_FGDB, outfeaturename + "_layer")
&amp;nbsp;&amp;nbsp;&amp;nbsp; COFIPSfeature = os.path.join(FRIS_FGDB, outfeaturename)
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.MakeFeatureLayer_management(feature, featurelayer)
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management (featurelayer, "NEW_SELECTION", CLAUSE)
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CopyFeatures_management(featurelayer, COFIPSfeature)
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Delete_management(featurelayer)&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;What is going on here?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 16:26:04 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/invalid-expression-error-in-loop/m-p/402294#M31676</guid>
      <dc:creator>JohnLay</dc:creator>
      <dc:date>2021-12-12T16:26:04Z</dc:date>
    </item>
    <item>
      <title>Re: Invalid Expression Error in Loop</title>
      <link>https://community.esri.com/t5/python-questions/invalid-expression-error-in-loop/m-p/402295#M31677</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Seems to be having a problem with your expression - maybe you have some null values?:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;CLAUSE = ('{0} = {1}'.format ("CO_FIPS", COFIPS))&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;If the script had problems with one of the other parameters, you'd likely get a msg similar to "INVALID PARAMETERS".&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Wayne&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;PS- By the way, there's a sample using the AddFieldDelimiters function here that appears to contain an error:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;AddFieldDelimiters (arcpy)&lt;/SPAN&gt;&lt;BR /&gt;&lt;A class="jive-link-external-small" href="http://resources.arcgis.com/en/help/main/10.2/index.html#//018v0000006p000000" rel="nofollow" target="_blank"&gt;http://resources.arcgis.com/en/help/main/10.2/index.html#//018v0000006p000000&lt;/A&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;# This appears to be in error- 'field_name' is used twice in the expression: sql_exp = """{0} = {1}""".format( &amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddFieldDelimiters('c:/data', field_name), &amp;nbsp;&amp;nbsp;&amp;nbsp; field_name)&amp;nbsp; # ...should likely be 'state_value', the only unused parameter. # Also, note this would only work for numerical fields; txt fields would be: #&amp;nbsp; """{0} = '{1}'""" so on and so forth... sql_exp = """{0} = {1}""".format( &amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddFieldDelimiters('c:/data', field_name), &amp;nbsp;&amp;nbsp;&amp;nbsp; state_value)&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 21 Apr 2014 12:28:32 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/invalid-expression-error-in-loop/m-p/402295#M31677</guid>
      <dc:creator>T__WayneWhitley</dc:creator>
      <dc:date>2014-04-21T12:28:32Z</dc:date>
    </item>
    <item>
      <title>Re: Invalid Expression Error in Loop</title>
      <link>https://community.esri.com/t5/python-questions/invalid-expression-error-in-loop/m-p/402296#M31678</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;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.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 21 Apr 2014 12:37:34 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/invalid-expression-error-in-loop/m-p/402296#M31678</guid>
      <dc:creator>JohnLay</dc:creator>
      <dc:date>2014-04-21T12:37:34Z</dc:date>
    </item>
  </channel>
</rss>

