dmckelly10

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

Discussion created by dmckelly10 on Nov 2, 2012
Latest reply on Nov 5, 2012 by dmckelly10
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

Outcomes