<?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: Construct a where clause when using the .SelectLayerByAttribute_management( in py in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/construct-a-where-clause-when-using-the/m-p/224996#M17396</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Mark's suggestion of using the cursor to build a list and iterating through the list will greatly improve the stability of the code.&amp;nbsp; Something like below should work (haven't tested it)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
import arcpy
import os

folder = r'C:\Your Desired Output Path'
rows = arcpy.SearchCursor(lyr)
list = []
nullList = ['',' ','None']
for row in rows:
&amp;nbsp;&amp;nbsp;&amp;nbsp; mesoID = str(row.getValue('MESO_ID'))
&amp;nbsp;&amp;nbsp;&amp;nbsp; if mesoID not in nullList:&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; list.append(mesoID)
del row
del rows

for item in list:
&amp;nbsp;&amp;nbsp;&amp;nbsp; item = ''.join(["'",item,"'"])
&amp;nbsp;&amp;nbsp;&amp;nbsp; query = ''.join(['\"MESO_ID\" = ',item])
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management("lyr","NEW_SELECTION",query)
&amp;nbsp;&amp;nbsp;&amp;nbsp; outFC = os.path.join(folder,''.join([item.strip("'"),'.shp']))
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CopyFeatures_management("lyr", outFC)

&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 11 Dec 2021 10:58:18 GMT</pubDate>
    <dc:creator>BruceBacia</dc:creator>
    <dc:date>2021-12-11T10:58:18Z</dc:date>
    <item>
      <title>Construct a where clause when using the .SelectLayerByAttribute_management( in pyhhon</title>
      <link>https://community.esri.com/t5/python-questions/construct-a-where-clause-when-using-the/m-p/224994#M17394</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Can somebody please help me to construct a where clause using a variable when using the arcpy.SelectLayerByAttribute_management( in pyhon.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;1. Here is some of my code:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;STRONG&gt;while row:&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; print row.MESO_ID&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; SelStr = '"' +'\"MESO_ID\" = ' + "'" +row.MESO_ID + "'" + '"'&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; print SelStr&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; outfeat = "p"+row.MESO_ID&lt;BR /&gt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Selected features, from "lyr" wher MESO_ID = row.MESO_ID&amp;nbsp;&amp;nbsp; &lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management("lyr", "NEW_SELECTION", '"' +'\"MESO_ID\" = ' + "'" +row.MESO_ID + "'" + '"')&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Write the selected features to a new featureclass&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CopyFeatures_management("lyr", outfeat)&lt;BR /&gt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; row = rows.next()&lt;/STRONG&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;2. When I run the script the SelStr prints the correct string but it keep on giving me an error at the:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;STRONG&gt;arcpy.SelectLayerByAttribute_management("lyr", "NEW_SELECTION", '"' +'\"MESO_ID\" = ' + "'" +row.MESO_ID + "'" + '"')&lt;/STRONG&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;line.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;3. I have also test it by entering the " "Meso_ID = '2010_20316' " value. This works perfect but since there are 25000 mesos, I cannot do them manually.&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Any ideas please?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Regards&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;David&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 02 Nov 2012 08:54:31 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/construct-a-where-clause-when-using-the/m-p/224994#M17394</guid>
      <dc:creator>DavidMcKelly</dc:creator>
      <dc:date>2012-11-02T08:54:31Z</dc:date>
    </item>
    <item>
      <title>Re: Construct a where clause when using the .SelectLayerByAttribute_management( in py</title>
      <link>https://community.esri.com/t5/python-questions/construct-a-where-clause-when-using-the/m-p/224995#M17395</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;You seem to be trying to run the SelectLayerByAttribute_management tool while inside a cursor.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;The cursor will lock the file, will it not, preventing the tool from getting access to the feature class?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I would suggest using the cursor to build a list of MESO_IDs in the feature class, &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;and then using the list to feed a loop that performs each selection by attibute.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;By the by, using python's format substitution can make for much neater code &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;than concatenating with plus signs, and all those triple quotes...&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 02 Nov 2012 12:49:45 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/construct-a-where-clause-when-using-the/m-p/224995#M17395</guid>
      <dc:creator>markdenil</dc:creator>
      <dc:date>2012-11-02T12:49:45Z</dc:date>
    </item>
    <item>
      <title>Re: Construct a where clause when using the .SelectLayerByAttribute_management( in py</title>
      <link>https://community.esri.com/t5/python-questions/construct-a-where-clause-when-using-the/m-p/224996#M17396</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Mark's suggestion of using the cursor to build a list and iterating through the list will greatly improve the stability of the code.&amp;nbsp; Something like below should work (haven't tested it)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
import arcpy
import os

folder = r'C:\Your Desired Output Path'
rows = arcpy.SearchCursor(lyr)
list = []
nullList = ['',' ','None']
for row in rows:
&amp;nbsp;&amp;nbsp;&amp;nbsp; mesoID = str(row.getValue('MESO_ID'))
&amp;nbsp;&amp;nbsp;&amp;nbsp; if mesoID not in nullList:&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; list.append(mesoID)
del row
del rows

for item in list:
&amp;nbsp;&amp;nbsp;&amp;nbsp; item = ''.join(["'",item,"'"])
&amp;nbsp;&amp;nbsp;&amp;nbsp; query = ''.join(['\"MESO_ID\" = ',item])
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management("lyr","NEW_SELECTION",query)
&amp;nbsp;&amp;nbsp;&amp;nbsp; outFC = os.path.join(folder,''.join([item.strip("'"),'.shp']))
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CopyFeatures_management("lyr", outFC)

&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 10:58:18 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/construct-a-where-clause-when-using-the/m-p/224996#M17396</guid>
      <dc:creator>BruceBacia</dc:creator>
      <dc:date>2021-12-11T10:58:18Z</dc:date>
    </item>
    <item>
      <title>Re: Construct a where clause when using the .SelectLayerByAttribute_management( in py</title>
      <link>https://community.esri.com/t5/python-questions/construct-a-where-clause-when-using-the/m-p/224997#M17397</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;instead of copy features you'll probably want to use featureClass to featureClass.&amp;nbsp; it looks like copy features requires that the feature class be in existence.&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
import arcpy
import os

folder = r'C:\Your Desired Output Path'
rows = arcpy.SearchCursor(lyr)
list = []
nullList = ['',' ','None']
for row in rows:
&amp;nbsp;&amp;nbsp;&amp;nbsp; mesoID = str(row.getValue('MESO_ID'))
&amp;nbsp;&amp;nbsp;&amp;nbsp; if mesoID not in nullList:&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; list.append(mesoID)
del row
del rows

for item in list:
&amp;nbsp;&amp;nbsp;&amp;nbsp; item = ''.join(["'",item,"'"])
&amp;nbsp;&amp;nbsp;&amp;nbsp; query = ''.join(['\"MESO_ID\" = ',item])
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.FeatureClassToFeatureClass_conversion (lyr,folder, ''.join([item.strip("'"), '.shp']), query,'','')
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 10:58:21 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/construct-a-where-clause-when-using-the/m-p/224997#M17397</guid>
      <dc:creator>BruceBacia</dc:creator>
      <dc:date>2021-12-11T10:58:21Z</dc:date>
    </item>
    <item>
      <title>Re: Construct a where clause when using the .SelectLayerByAttribute_management( in py</title>
      <link>https://community.esri.com/t5/python-questions/construct-a-where-clause-when-using-the/m-p/224998#M17398</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thank you Bruce and Mark for your useful response. I used you advice and it works fine now.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Regards&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 05 Nov 2012 09:03:05 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/construct-a-where-clause-when-using-the/m-p/224998#M17398</guid>
      <dc:creator>DavidMcKelly</dc:creator>
      <dc:date>2012-11-05T09:03:05Z</dc:date>
    </item>
  </channel>
</rss>

