Python Script for "Select by Attributes "

4304
6
10-27-2010 01:40 PM
GeorgeBentley
New Contributor
Hello Everyone,

I am working on a python script that is progressing nicely.  I am having an issue when I am trying to use the "Select by Attribute" tool.  I have posted the post below for reference.  In the BID field of the Final_Intersection file I want to select where BID = 1 and then export these selected records to a new file.  I would greatly appreciate any help that could be offered.

The error that is returned is:

Traceback (most recent call last):
  File "F:/Dr_Cromley/Python_Work/GCB_DZ_Trial.py", line 62, in <module>
    gp.SelectLayerByAttribute("temp_lyr", "NEW_SELECTION","[BID] = '1'")
ExecuteError: ERROR 000358: Invalid expression
Failed to execute (SelectLayerByAttribute).

Here is a version of the Python script:

import sys  # Imports the sys module.
import os # Imports the operating system module
import arcgisscripting # Imports the arcgisscripting module
import string # Imports the string module
import math # Imports the math module

gp = arcgisscripting.create(9.3) # Creates the geoprocessor by using the create method of the arcgisscripting module.

gp.SetProduct('ArcInfo') # Sets the selection of ArcTools to the ArcEditor level.  Not necessary in this case but included for demonstration.

gp.OverwriteOutput = 1 # Setting the value to 1 allows for outputs to be overwritten.  A value of 0 would prevent outputs from being overwritten and make it more difficult to run the script more than once.

gp.Workspace = r"D:\Dr_Cromley\Python_Work\Python_Data"
##
###gp.Workspace = sys.argv[1] # Sets the workspace for the data so that the user does not need to type in full pathname.  ## Will be set as system parameter.
##
###Sourcefile = sys.argv[2] # Creates a system parameter for user to input the source file that will be used in ArcGIS.
###Targetfile = sys.argv[3] # Creates a system parameter for the user to input the target file that will be used in ArcGIS.
##
###gp.AddToolBox (r"C:\Program Files\ArcGIS\ArcToolbox\Toolboxes\Data Management Tools.tbx") # Adds the tools within the Data Management ToolBox
###gp.AddToolBox (r"C:\Program Files\ArcGIS\ArcToolbox\Toolboxes\Analysis Tools.tbx") # Adds the tools within the Analysis ToolBox
##
Srcfile = r"D:\Dr_Cromley\Python_Work\Python_Data\CT_POP_LC_FIX_FIN.shp" #Setting the source shapefile equal to the census tract shapefile for the testing purposes.
Trgtfile = r"D:\Dr_Cromley\Python_Work\Python_Data\BG_POP_LC_FIX_FIN.shp" #Setting the target shapefile equal to the block group shapefile for the testing purposes.
Cntrlfile = r"D:\Dr_Cromley\Python_Work\Python_Data\landcover_polygon.shp"

##gp.AddField_management (Srcfile, "S_Area", "DOUBLE", 15, 10, "", "", "", "", "") #Adding a double field with scale of 15 and precision of 10 to the intersection file.
##
##gp.CalculateField_management (Srcfile, "S_Area", "(float(!SHAPE.AREA!))", "PYTHON_9.3", "")  #Setting the Calculate Field tool to calculate the area of the census tract or source file inside of the Src_Area field created above.
##
##
##gp.Dissolve_management (Cntrlfile, "LC_Control_Dissolve.shp", "BID", "", "MULTI_PART", "UNSPLIT_LINES")
##
##Dissolve_Output1 = "LC_Control_Dissolve.shp"
##
##gp.Dissolve_management (Dissolve_Output1, "LC_Control_Dissolve_2.shp", "BID", "", "MULTI_PART", "UNSPLIT_LINES")
##
##Inputs = "%s;%s" % (Srcfile,Trgtfile) #Uses variable substitution to create one input variable to be used in the intersection tool so to avoid the problem of having the script crash when there are more than one input variables.
##
##Intersection_Layer = "Intersection_Source_Target_Dasy.shp"  #Creates a variable name for the Intersection_Source_Target_Dasy.shp that is a created as a result of the first intersection.
##
##gp.Intersect_analysis (Inputs, Intersection_Layer, "ALL", "", "INPUT")  #Runs the intersect for the areal interpolation analysis.  Due to the problems with the multiple input variables, the file along with their pathnames had to be used to continue  writing the script.
##
##LC_Control_Dissolve_2 = "LC_Control_Dissolve_2.shp"
##
##Inputs_2nd = "%s;%s" % (Intersection_Layer, LC_Control_Dissolve_2)
##
Final_Intersection = "Intersection_Intrsct_LCCntrlDsslve2.shp"
##
##gp.Intersect_analysis (Inputs_2nd, Final_Intersection, "ALL", "", "INPUT")
##
##gp.AddField_management (Final_Intersection, "FI_Area", "DOUBLE", 15, 10, "", "", "", "", "") #Adding a double field with scale of 15 and precision of 10 to the intersection file.
##
##gp.CalculateField_management (Final_Intersection, "FI_Area", "(float(!SHAPE.AREA!))", "PYTHON_9.3", "")  #Setting the Calculate Field tool to calculate the area of the census tract or source file inside of the Src_Area field created above.
##


gp.MakeFeatureLayer(Final_Intersection,"temp_lyr")


gp.SelectLayerByAttribute("temp_lyr", "NEW_SELECTION","[BID] = '1'")
0 Kudos
6 Replies
ChrisSnyder
Regular Contributor III
This is a stupid syntax thing with .shp and .dbf files, but try this:

gp.SelectLayerByAttribute("temp_lyr", "NEW_SELECTION","\"BID"\" = '1'")

if it was in FGDB it would just be:

gp.SelectLayerByAttribute("temp_lyr", "NEW_SELECTION","BID = '1'")
0 Kudos
GeorgeBentley
New Contributor
Thanks, I will try this tomorrow morning and reply as to the outcome.

George
0 Kudos
GeorgeBentley
New Contributor
Hi,

I gave this line a try this morning

gp.SelectLayerByAttribute("temp_lyr", "NEW_SELECTION","\"BID"\" = '1'")

A syntax error of "There's an error in your program:  unexpected character after line continuation character." is returned.

I believe it is the second \ that is creating the problem but am unsure how to fix it.

Thanks again,
George
0 Kudos
ChrisSnyder
Regular Contributor III
George, Sorry to lead you astray:

Seems the "\"\ thing for .shp and .dbf formats is no longer neccessary in v9.3.1. Try this:

gp.SelectLayerByAttribute("temp_lyr", "NEW_SELECTION", "BID = '1'")

or if the BID field is numeric, then just this:

gp.SelectLayerByAttribute("temp_lyr", "NEW_SELECTION","BID = 1")
0 Kudos
GeorgeBentley
New Contributor
Chris,

This suggestion worked.  Thanks for your help, it is greatly appreciated.


Best,
George
0 Kudos
KONPETROV
Occasional Contributor III

is there a way to put also BIS = max(c)? Let's say that c is a numeric field

0 Kudos