I've been having a problem with arcpy in that the simple line:
arcpy.SelectLayerByAttribute_management ("TEMP_LINES")
produces this response: Runtime error : ERROR 000358: Invalid expression Failed to execute (SelectLayerByAttribute).
I've worked out that this error only occurs when a cursor is set, like this:
lineCursor = arcpy.UpdateCursor("TEMP_LINES","","","combined_i")
Can I not use select by attribute and the cursor or do I have one of them set up wrong?
EDIT: whole code:
arcpy.MakeFeatureLayer_management (Lines,"TEMP_LINES")
lineCursor = arcpy.UpdateCursor("TEMP_LINES","","","combined_i")
for row in lineCursor:
arcpy.SelectLayerByAttribute_management ("TEMP_LINES")
arcpy.SelectLayerByLocation_management (Polygons, "INTERSECT", "TEMP_LINES")
arcpy.FeatureClassToFeatureClass_conversion (Polygons, "temp_polys")
polyCursor = arcpy.SearchCursor("temp_polys")
blocks_list = []
for row in cursor:
blocks_list.append(row.getValue("BLOCK"))
blocks_str = ', '.join(blocks_list)
lineCursor.BLOCK = blocks_str
2 Answers 2
As stated by others you have not provided a selection criteria for arcpy.SelectLayerByAttribute_management()
. Additionally using arcpy.SelectLayerByAttribute_management()
within an Update Cursor is only going to apply to a single row and does not really make sense to do.
If I understand what you are trying to accomplish, I think you need to rework your logic in this script. Perhaps run the arcpy.SelectLayerByLocation_management()
previous to creating your cursors and use a where_clause
as an argument to your arcpy.UpdateCursor
to limit the number of records that you are searching and updating against.
The SelectLayerByAttribute_management
tool in your script is trying to select all features within the feature layer, whereas the cursor accesses one row at a time within the feature. If you would still like to use the tool within the cursor then there is a bit of an adjustment needed. I should make mention that this adjustment assumes the field "combined_i"
is not unique.
lineCursor = arcpy.UpdateCursor("TEMP_LINES","","","combined_i; unique_field")
cols = arcpy.ListFields("TEMP_LINES")
for row in lineCursor:
for col in cols:
if col.name == "unique_field":
objstr = row.getValue(col.name)
arcpy.SelectLayerByAttribute_management("TEMP_LINES", "NEW_SELECTION", ("%s = %s" % (col.name, objstr)))
.
.
.
Allow me to explain the additional code in more detail:
cols = arcpy.ListFields("TEMP_LINES")
for col in cols:
if col.name == "unique_field":
objstr = row.getValue(col.name)
First, list all fields in "TEMP_LINES"
, then loop through the fields and find a field named "unique_field"
. If true then set objstr
equal to the value of "unique_field" in the current row.
arcpy.SelectLayerByAttribute_management ("TEMP_LINES", "NEW_SELECTION", ("%s = %s" % (col.name, objstr)))
Instead of trying to select all features in "TEMP_LINES"
this will only select the feature with the unique value in "unique_field"
at the current row.
Explore related questions
See similar questions with these tags.
SelectLayerByAttribute_management