Construct a where clause when using the .SelectLayerByAttribute_management( in pyhhon

478
4
11-02-2012 01:54 AM
DavidMcKelly
New Contributor
Hi

Can somebody please help me to construct a where clause using a variable when using the arcpy.SelectLayerByAttribute_management( in pyhon.

1. Here is some of my code:

while row:
    print row.MESO_ID
    SelStr = '"' +'\"MESO_ID\" = ' + "'" +row.MESO_ID + "'" + '"'
    print SelStr
    outfeat = "p"+row.MESO_ID

    # Selected features, from "lyr" wher MESO_ID = row.MESO_ID  
    arcpy.SelectLayerByAttribute_management("lyr", "NEW_SELECTION", '"' +'\"MESO_ID\" = ' + "'" +row.MESO_ID + "'" + '"')
    
    # Write the selected features to a new featureclass
    arcpy.CopyFeatures_management("lyr", outfeat)

    row = rows.next()


2. When I run the script the SelStr prints the correct string but it keep on giving me an error at the:
     
    arcpy.SelectLayerByAttribute_management("lyr", "NEW_SELECTION", '"' +'\"MESO_ID\" = ' + "'" +row.MESO_ID + "'" + '"')
    
line.

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.  

Any ideas please?

Regards
David
Tags (2)
0 Kudos
4 Replies
markdenil
Occasional Contributor III
You seem to be trying to run the SelectLayerByAttribute_management tool while inside a cursor.
The cursor will lock the file, will it not, preventing the tool from getting access to the feature class?

I would suggest using the cursor to build a list of MESO_IDs in the feature class,
and then using the list to feed a loop that performs each selection by attibute.

By the by, using python's format substitution can make for much neater code
than concatenating with plus signs, and all those triple quotes...
0 Kudos
BruceBacia
Occasional Contributor
Mark's suggestion of using the cursor to build a list and iterating through the list will greatly improve the stability of the code.  Something like below should work (haven't tested it)

import arcpy
import os

folder = r'C:\Your Desired Output Path'
rows = arcpy.SearchCursor(lyr)
list = []
nullList = ['',' ','None']
for row in rows:
    mesoID = str(row.getValue('MESO_ID'))
    if mesoID not in nullList:    
        list.append(mesoID)
del row
del rows

for item in list:
    item = ''.join(["'",item,"'"])
    query = ''.join(['\"MESO_ID\" = ',item])
    arcpy.SelectLayerByAttribute_management("lyr","NEW_SELECTION",query)
    outFC = os.path.join(folder,''.join([item.strip("'"),'.shp']))
    arcpy.CopyFeatures_management("lyr", outFC)

0 Kudos
BruceBacia
Occasional Contributor
instead of copy features you'll probably want to use featureClass to featureClass.  it looks like copy features requires that the feature class be in existence. 

import arcpy
import os

folder = r'C:\Your Desired Output Path'
rows = arcpy.SearchCursor(lyr)
list = []
nullList = ['',' ','None']
for row in rows:
    mesoID = str(row.getValue('MESO_ID'))
    if mesoID not in nullList:    
        list.append(mesoID)
del row
del rows

for item in list:
    item = ''.join(["'",item,"'"])
    query = ''.join(['\"MESO_ID\" = ',item])
    arcpy.FeatureClassToFeatureClass_conversion (lyr,folder, ''.join([item.strip("'"), '.shp']), query,'','')
0 Kudos
DavidMcKelly
New Contributor
Thank you Bruce and Mark for your useful response. I used you advice and it works fine now.

Regards
0 Kudos