where_clause = ("Status = 'U'") arcpy.MakeFeatureLayer_management("Crimes", "Crimes_Lyr") arcpy.SelectLayerByAttribute_management("Crimes_Lyr", "NEW_SELECTION", where_clause) print arcpy.GetCount_management("Crimes_Lyr")
fld = arcpy.AddFieldDelimiters("Crimes", "Status") where_clause = (fld + " = 'U'") arcpy.MakeFeatureLayer_management("Crimes", "Crimes_Lyr") arcpy.SelectLayerByAttribute_management("Crimes_Lyr", "NEW_SELECTION", where_clause) print arcpy.GetCount_management("Crimes_Lyr")
Writing a python script and attempting to use the SelectLayerByAttribute_management command.I've tried it several different ways and keep getting a syntax error on the variable set, or if the variable set works, get invalid expression. I am querying against a file geodatabase.python snippet:where_clause = (r"Status = 'U'")arcpy.SelectLayerByAttribute_management("Crimes", "NEW_SELECTION", where_clause)I'd like to add .outputCount to this to see if there are any rows selected. I have done this once in the python window but can't remember how I did it now and didn't put it in my script when it worked.I've also tried various ways of putting quotes and single quotes with and without the r or the parenthesis. Syntax error or it doesn't get passed properly to the query and gives the Invalid expression.Help!Forgot to add: In the ArcMap window using the Select by Attributes dialog, the proper syntax is "Status" = 'U' and it works as expected. Just can't get the translation to python to work properly.
arcpy.SelectLayerByAttribute_management("Crimes", "NEW_SELECTION", "\"Status\"='U'")
whereClause = "ADDRNO = 1930 AND ADDRSTREET = 'BIG OWL' AND ADDRSUFFIX = 'ROAD'"
Thanks for the suggestion. When I tried it, I got a new error:<class 'arcgisscripting.ExecuteError'>: Failed to execute. Parameters are not valid.ERROR 000825: The value is not a layer or table viewERROR 000840: The value is not a Raster Layer.ERROR 000840: The value is not a Mosaic Layer.Failed to execute (SelectLayerByAttribute).Failed to execute (TestAddressZoomTo).Any other suggestions? Again, I am trying to select 3 address fields and zoom to the selected feature. The first field is numeric, the other 2 fields are text. I also tried changing the script so that the 3 fields were user defined parameters. Then I used the GetParameterAsText function within the SelectLayerByAttribute where clause. I got the same error as listed above. Here is the revised script I was trying:# Script argumentsPAR1 = arcpy.GetParameterAsText(0)PAR2 = arcpy.GetParameterAsText(1)PAR3 = arcpy.GetParameterAsText(2)#Select by Attributesparcels = "C:\PreApp Maps\Parcels.lyr"whereClause = "ADDRNO = PAR1 AND ADDRSTREET = PAR2 AND ADDRSUFFIX = PAR3"arcpy.SelectLayerByAttribute_management (parcels, "NEW_SELECTION", whereClause) #Zoom to Selectedmxd = arcpy.mapping.MapDocument(r"Current")df = arcpy.mapping.ListDataFrames(mxd, "Boulder County")[0]df.zoomToSelectedFeatures()#arcpy.RefreshActiveView()
arcpy.env.workspace ="C:\PreApp Maps" arcpy.MakeFeatureLayer_management("Parcels", "lyr") ... arcpy.SelectLayerByAttribute_management ("lyr", "NEW_SELECTION", whereClause)
have you tested the query through the standard select by attributes interface in ArcMap? Have you tried it incrementally (ie add one piece, then the second etc)?
"\"ADDRNO\" = 2 AND \"ADDRSTREET\" = 'PONDEROSA' AND \"ADDRSUFFIX\" = 'LANE'"
You will either need to escape your double-quotes with backslashes or single-quotes. Backslashes are probably the most obvious:"\"ADDRNO\" = 2 AND \"ADDRSTREET\" = 'PONDEROSA' AND \"ADDRSUFFIX\" = 'LANE'"
whereClause = "\"ADDRNO\" = " + str(PAR1_int) + " AND \"ADDRSTREET\" = '" + str(PAR2) + "' AND \"ADDRSUFFIX\" = '" + str(PAR3) +"'"
Attached is a zip file containing a portion of the parcel shapefile. Here is the code I was using to just try to test the Select by Attributes function:#Select by Attributesarcpy.env.workspace ="C:\PreApp Maps"arcpy.MakeFeatureLayer_management("Parcels.shp", "parcels_lyr")whereClause = "\"ADDRNO\" = 2 AND \"ADDRSTREET\" = 'PONDEROSA' AND \"ADDRSUFFIX\" = 'LANE'"arcpy.SelectLayerByAttribute_management ("parcels_lyr", "NEW_SELECTION", whereClause)Ideally, I would like to use 3 user defined parameters to get the parcel selected which I have been using this script that doesn't work:# Script argumentsPAR1 = arcpy.GetParameterAsText(0)PAR2 = arcpy.GetParameterAsText(1)PAR3 = arcpy.GetParameterAsText(2)#Parse first parameter to numericPAR1_int =int(PAR1) #Select by Attributesarcpy.env.workspace ="C:\PreApp Maps"arcpy.MakeFeatureLayer_management("Parcels.shp", "parcels_lyr")whereClause = "ADDRNO = PAR1_int AND ADDRSTREET = PAR2 AND ADDRSUFFIX = PAR3" arcpy.SelectLayerByAttribute_management ("parcels_lyr", "NEW_SELECTION", whereClause)
PAR1 = arcpy.GetParameterAsText(0) PAR2 = arcpy.GetParameterAsText(1) PAR3 = arcpy.GetParameterAsText(2) #Select by Attributes arcpy.env.workspace ="C:\PreApp Maps" arcpy.MakeFeatureLayer_management("Parcels.shp", "parcels_lyr") whereClause = "ADDRNO =" + PAR1 +" AND ADDRSTREET = '"+ PAR2+"' AND ADDRSUFFIX ='" + PAR3 +"'" arcpy.SelectLayerByAttribute_management ("parcels_lyr", "NEW_SELECTION", whereClause)
Were you able to get the code you posted to work? The script runs successfully but no parcel is selected so the map does not zoom to the selected parcel. I copied your code exactly as you posted it and I'm still not successful. I also tried including the "parse first paramter to numeric" suggestion as mentioned on 8/16/2011 but then I get an error that that it cannot concatenate 'str' and 'int' objects.
Los miembros registrados pueden publicar, seguir actualizaciones y más. ¿Nuevo aquí? Regístrate gratis.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.