<?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 SelectLayerByAttribute nto working inside a loop in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/selectlayerbyattribute-nto-working-inside-a-loop/m-p/102784#M7920</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi everyone, &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;(system is 9.3.1)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I am trying to create a script that takes in LiDAR, .las files, calculates the heights from the ground, and returns point files of user defined height categories. If you guys need it I can post the whole code, but its fairly long. The Idea is that the user types in a list of strings, for example low med and high, and then defines those heights as say less than 5, between 5 and 10 and above 10, and then gets three point files out, corresponding to each height category. This is the part of the code that is failing: &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Note: name_of_hights = 'low med high'.split()&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;# Make a layer from the feature class
&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.MakeFeatureLayer("test.gdb\\raster_height_point","lyr") 

&amp;nbsp;&amp;nbsp;&amp;nbsp; counter = 0
&amp;nbsp;&amp;nbsp;&amp;nbsp; while counter &amp;lt; len(name_of_hights):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cat_name = str(name_of_hights[counter])
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.SelectLayerByAttribute("lyr", "NEW_SELECTION", " ' " + cat + " ' = ' " + cat_name + " ' ")

&amp;nbsp;&amp;nbsp;&amp;nbsp; # Write the selected features to a new featureclass
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; path1="test.gdb\\raster_height_point_"
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; path2=name_of_hights[counter]
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; path=path1+path2
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.CopyFeatures("lyr", path)

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print(cat_name)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; counter=counter+1
&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;It will fail with this:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Executing: MakeFeatureLayer test.gdb\raster_height_point lyr1 # # "POINTID POINTID VISIBLE NONE;GRID_CODE GRID_CODE VISIBLE NONE;cat cat VISIBLE NONE"&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Start Time: Mon May 14 14:05:59 2012&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Executed (MakeFeatureLayer) successfully.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;End Time: Mon May 14 14:06:00 2012 (Elapsed Time: 1.00 seconds)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;If I do this #gp.MakeFeatureLayer("test.gdb\\raster_height_point","lyr") &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;then it will run, however it will not ever complete the loop, it never runs print(cat_name) at the end of the loop. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;If I have gp.MakeFeatureLayer("test.gdb\\raster_height_point","lyr")&amp;nbsp; and&amp;nbsp; #gp.SelectLayerByAttribute("lyr", "NEW_SELECTION", " ' " + cat + " ' = ' " + cat_name + " ' ") then the loop runs through and prints fine. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;If I replace the loop with if statements and have gp.SelectLayerByAttribute("lyr", "NEW_SELECTION", " cat = low ") etc it works fine, makes the point files and everything. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;cat is a category in the attributes table which has already been defined, as low med or high. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;But because the number of categories needs to change I cant do it with if statements.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;does anyone have any ideas as to what I have got wrong in here?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks for any help you can give me&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Mon, 14 May 2012 01:24:05 GMT</pubDate>
    <dc:creator>NickDavies</dc:creator>
    <dc:date>2012-05-14T01:24:05Z</dc:date>
    <item>
      <title>SelectLayerByAttribute nto working inside a loop</title>
      <link>https://community.esri.com/t5/python-questions/selectlayerbyattribute-nto-working-inside-a-loop/m-p/102784#M7920</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi everyone, &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;(system is 9.3.1)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I am trying to create a script that takes in LiDAR, .las files, calculates the heights from the ground, and returns point files of user defined height categories. If you guys need it I can post the whole code, but its fairly long. The Idea is that the user types in a list of strings, for example low med and high, and then defines those heights as say less than 5, between 5 and 10 and above 10, and then gets three point files out, corresponding to each height category. This is the part of the code that is failing: &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Note: name_of_hights = 'low med high'.split()&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;# Make a layer from the feature class
&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.MakeFeatureLayer("test.gdb\\raster_height_point","lyr") 

&amp;nbsp;&amp;nbsp;&amp;nbsp; counter = 0
&amp;nbsp;&amp;nbsp;&amp;nbsp; while counter &amp;lt; len(name_of_hights):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cat_name = str(name_of_hights[counter])
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.SelectLayerByAttribute("lyr", "NEW_SELECTION", " ' " + cat + " ' = ' " + cat_name + " ' ")

&amp;nbsp;&amp;nbsp;&amp;nbsp; # Write the selected features to a new featureclass
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; path1="test.gdb\\raster_height_point_"
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; path2=name_of_hights[counter]
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; path=path1+path2
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.CopyFeatures("lyr", path)

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print(cat_name)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; counter=counter+1
&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;It will fail with this:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Executing: MakeFeatureLayer test.gdb\raster_height_point lyr1 # # "POINTID POINTID VISIBLE NONE;GRID_CODE GRID_CODE VISIBLE NONE;cat cat VISIBLE NONE"&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Start Time: Mon May 14 14:05:59 2012&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Executed (MakeFeatureLayer) successfully.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;End Time: Mon May 14 14:06:00 2012 (Elapsed Time: 1.00 seconds)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;If I do this #gp.MakeFeatureLayer("test.gdb\\raster_height_point","lyr") &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;then it will run, however it will not ever complete the loop, it never runs print(cat_name) at the end of the loop. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;If I have gp.MakeFeatureLayer("test.gdb\\raster_height_point","lyr")&amp;nbsp; and&amp;nbsp; #gp.SelectLayerByAttribute("lyr", "NEW_SELECTION", " ' " + cat + " ' = ' " + cat_name + " ' ") then the loop runs through and prints fine. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;If I replace the loop with if statements and have gp.SelectLayerByAttribute("lyr", "NEW_SELECTION", " cat = low ") etc it works fine, makes the point files and everything. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;cat is a category in the attributes table which has already been defined, as low med or high. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;But because the number of categories needs to change I cant do it with if statements.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;does anyone have any ideas as to what I have got wrong in here?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks for any help you can give me&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 14 May 2012 01:24:05 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/selectlayerbyattribute-nto-working-inside-a-loop/m-p/102784#M7920</guid>
      <dc:creator>NickDavies</dc:creator>
      <dc:date>2012-05-14T01:24:05Z</dc:date>
    </item>
    <item>
      <title>Re: SelectLayerByAttribute nto working inside a loop</title>
      <link>https://community.esri.com/t5/python-questions/selectlayerbyattribute-nto-working-inside-a-loop/m-p/102785#M7921</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Nick,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Try re-writing the whereclause of your select statement to: "\"cat\" = '%s'" % cat_name&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Attribute names need to be enclosed in double quotes, and string values in single quotes.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 14 May 2012 11:45:30 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/selectlayerbyattribute-nto-working-inside-a-loop/m-p/102785#M7921</guid>
      <dc:creator>BruceNielsen</dc:creator>
      <dc:date>2012-05-14T11:45:30Z</dc:date>
    </item>
    <item>
      <title>Re: SelectLayerByAttribute nto working inside a loop</title>
      <link>https://community.esri.com/t5/python-questions/selectlayerbyattribute-nto-working-inside-a-loop/m-p/102786#M7922</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks for that Bruce, it worked grate.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 14 May 2012 21:18:54 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/selectlayerbyattribute-nto-working-inside-a-loop/m-p/102786#M7922</guid>
      <dc:creator>NickDavies</dc:creator>
      <dc:date>2012-05-14T21:18:54Z</dc:date>
    </item>
  </channel>
</rss>

