<?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: Select Layer by Attribute using an expression failing in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/select-layer-by-attribute-using-an-expression/m-p/261690#M20124</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Is the overall goal to simply count the instances of a certain value in a field? &amp;nbsp;If so, I would just use a search cursor to count the values.&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 15 Aug 2017 19:24:27 GMT</pubDate>
    <dc:creator>MitchHolley1</dc:creator>
    <dc:date>2017-08-15T19:24:27Z</dc:date>
    <item>
      <title>Select Layer by Attribute using an expression failing</title>
      <link>https://community.esri.com/t5/python-questions/select-layer-by-attribute-using-an-expression/m-p/261689#M20123</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;P&gt;Greetings, I am writing some code that puts in an expression into a SelectLayerByAttribute statement and am running into a problem. I am generating a running sequence of numbers (closNum) that are made into text. (using an SDE geodatabase FYI)&lt;/P&gt;&lt;P&gt;closNum = "'" + '{:05d}'.format(i) + "'"&lt;/P&gt;&lt;P&gt;When I print that out it looks like closNum = '00000'&lt;/P&gt;&lt;P&gt;I generate an expression statement expression = '"' + ' LABEL = ' + "'" + closNum + "'"+ '"' which prints out as " LABEL = '00000'"&lt;/P&gt;&lt;P&gt;What I would like to do is put that into my layer selection query arcpy.SelectLayerByAttribute_management(closureLyr, "NEW_SELECTION", expression)&lt;/P&gt;&lt;P&gt;So when I run that, I get an invalid expression error, but if I run arcpy.SelectLayerByAttribute_management(closureLyr, "NEW_SELECTION", " LABEL = '00000'"), it works just fine.&lt;/P&gt;&lt;P&gt;Somehow I am missing something but I can't figure out what.&lt;/P&gt;&lt;P&gt;Any help would be awesome&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Actual code below&lt;/P&gt;&lt;P&gt;import string, arcpy&lt;BR /&gt;from arcpy import env&lt;BR /&gt;arcpy.env.overwriteOutput = True&lt;/P&gt;&lt;P&gt;inFC = 'Database Connections/GISTLMD on DNRHLN6341 as TLMD_FMB_MGR - FMB_RoadInventory.sde/GISTLMD.TLMD_DO.FMB_RoadClosures'&lt;BR /&gt;closureLyr = 'closureLyr'&lt;BR /&gt;count = 1&lt;BR /&gt;arcpy.MakeFeatureLayer_management(inFC,closureLyr)&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;for i in range(0,10,1):&lt;BR /&gt;#&amp;nbsp;&amp;nbsp;print '{:05d}'.format(i)&lt;BR /&gt;&amp;nbsp;closNum = "'" +&amp;nbsp; '{:05d}'.format(i) + "'"&lt;BR /&gt;&amp;nbsp;expression = '"LABEL = '&amp;nbsp; +&amp;nbsp; closNum +&amp;nbsp; '"'&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt;&amp;nbsp;arcpy.AddMessage(expression)&lt;BR /&gt;&amp;nbsp;arcpy.SelectLayerByAttribute_management(closureLyr, "NEW_SELECTION", expression)&lt;BR /&gt;&amp;nbsp;# arcpy.SelectLayerByAttribute_management(closureLyr, "NEW_SELECTION", "LABEL = '00000'")&lt;BR /&gt;&amp;nbsp;result = arcpy.GetCount_management(closureLyr)&lt;BR /&gt;&amp;nbsp;cnt = int(result.getOutput(0))&lt;BR /&gt;&amp;nbsp;if cnt == 0:&lt;BR /&gt;&amp;nbsp;&amp;nbsp;arcpy.AddMessage(expression + ' is missing')&lt;BR /&gt;&amp;nbsp;&amp;nbsp;count = count + 1&lt;BR /&gt;&amp;nbsp;#count = count + 1&lt;BR /&gt;&amp;nbsp;if count == 5 :&lt;BR /&gt;&amp;nbsp;&amp;nbsp;break&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Donna&lt;/P&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 15 Aug 2017 18:12:14 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/select-layer-by-attribute-using-an-expression/m-p/261689#M20123</guid>
      <dc:creator>DonnaRiebe</dc:creator>
      <dc:date>2017-08-15T18:12:14Z</dc:date>
    </item>
    <item>
      <title>Re: Select Layer by Attribute using an expression failing</title>
      <link>https://community.esri.com/t5/python-questions/select-layer-by-attribute-using-an-expression/m-p/261690#M20124</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Is the overall goal to simply count the instances of a certain value in a field? &amp;nbsp;If so, I would just use a search cursor to count the values.&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 15 Aug 2017 19:24:27 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/select-layer-by-attribute-using-an-expression/m-p/261690#M20124</guid>
      <dc:creator>MitchHolley1</dc:creator>
      <dc:date>2017-08-15T19:24:27Z</dc:date>
    </item>
    <item>
      <title>Re: Select Layer by Attribute using an expression failing</title>
      <link>https://community.esri.com/t5/python-questions/select-layer-by-attribute-using-an-expression/m-p/261691#M20125</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I am just trying to flag gaps in the sequence so when I get new records I can fill in the gaps.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;d.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 15 Aug 2017 19:27:42 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/select-layer-by-attribute-using-an-expression/m-p/261691#M20125</guid>
      <dc:creator>DonnaRiebe</dc:creator>
      <dc:date>2017-08-15T19:27:42Z</dc:date>
    </item>
    <item>
      <title>Re: Select Layer by Attribute using an expression failing</title>
      <link>https://community.esri.com/t5/python-questions/select-layer-by-attribute-using-an-expression/m-p/261692#M20126</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;So, if a closNum is not 00000, 11111, 22222, etc... than it needs to be updated?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 15 Aug 2017 19:33:26 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/select-layer-by-attribute-using-an-expression/m-p/261692#M20126</guid>
      <dc:creator>MitchHolley1</dc:creator>
      <dc:date>2017-08-15T19:33:26Z</dc:date>
    </item>
    <item>
      <title>Re: Select Layer by Attribute using an expression failing</title>
      <link>https://community.esri.com/t5/python-questions/select-layer-by-attribute-using-an-expression/m-p/261693#M20127</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Yes, I then equipped with a list of labels that are aren't currently being used, as field folk add records, I can add a unique label to the record.&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 16 Aug 2017 14:29:33 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/select-layer-by-attribute-using-an-expression/m-p/261693#M20127</guid>
      <dc:creator>DonnaRiebe</dc:creator>
      <dc:date>2017-08-16T14:29:33Z</dc:date>
    </item>
    <item>
      <title>Re: Select Layer by Attribute using an expression failing</title>
      <link>https://community.esri.com/t5/python-questions/select-layer-by-attribute-using-an-expression/m-p/261694#M20128</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I finally figured out what the problem was, the expression statement wasn't formatted correctly.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;original - expression = '"LABEL = '&amp;nbsp; +&amp;nbsp; closNum +&amp;nbsp; '"' since the orginal closNum variable had the single quotes already in it.&lt;/P&gt;&lt;P&gt;revised (after I removed the single quotes from the closNum variable) - expression ="Label = \' " + closNum + "\'"&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;&lt;P&gt;Donna Riebe&lt;/P&gt;&lt;P&gt;Montana DNRC OIT/GIS&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 16 Aug 2017 14:33:33 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/select-layer-by-attribute-using-an-expression/m-p/261694#M20128</guid>
      <dc:creator>DonnaRiebe</dc:creator>
      <dc:date>2017-08-16T14:33:33Z</dc:date>
    </item>
  </channel>
</rss>

