I have a problem concerning the selection by attribute with python on arcmap.
I use this code:
import arcpy
from arcpy import env
arcpy.env.workspace= "E:\cities"
Mi=(r"E:\cities\rivers.shp")
rows = arcpy.SearchCursor(Mi,'"NAME" = \'Mississippi\'')
row = rows.next()
largeur = row.getValue("MILES")
arcpy.selectLayerByAttribute_management("rivers","New_SELECTION",'"MILES" >' + str(largeur))
And i got this error:
Traceback (most recent call last): File "E:\cities\scripts\Selection.py", line 8, in arcpy.selectLayerByAttribute_management("rivers","New_SELECTION",'"MILES"
' + str(largeur)) AttributeError: 'module' object has no attribute 'selectLayerByAttribute_management'
Échec de l’exécution de (SelectionByAttributes)
Can u help me please??
1 Answer 1
To help debug this I would suggest cleaning up the code a bit:
import arcpy
from arcpy import env
arcpy.env.workspace= r"E:\cities" # added raw string indicator `r`
Mi= r"E:\cities\rivers.shp" # removed parentheses - (r"E:\cities\rivers.shp") is a tuple, not a string
rows = arcpy.SearchCursor(Mi,'"NAME" = \'Mississippi\'')
row = rows.next()
largeur = row.getValue("MILES")
where = '"MILES" >' + str(largeur) # let's move the SQL selection clause out here so we can see it
print where
arcpy.selectLayerByAttribute_management("rivers","New_SELECTION",where)
I've removed the parentheses from your variable Mi
to make it a string instead of a tuple and moved your SQL query string into a new variable called where
- this way we can use a print
statement to see what your query looks like. Run this and see if you can determine where your problem is.
-
1In order to use
selectLayerByAttribute_management
, your reference to "rivers" will need to be a layer. Therefore, it would probably be better to remove the reference to "Mi" and change your reference to a "rivers" layer by using the notationarcpy.MakeFeatureLayer_management(r"E:\cities\rivers.shp","rivers")
RyanKDalton– RyanKDalton2013年12月03日 18:28:33 +00:00Commented Dec 3, 2013 at 18:28 -
See related answer in Select By Atribute using python script on .Shp file getting error 000840 the value is not a raster layerRyanKDalton– RyanKDalton2013年12月04日 19:59:52 +00:00Commented Dec 4, 2013 at 19:59