Select to view content in your preferred language

Search Cursor Structure

826
3
Jump to solution
08-07-2013 06:29 AM
JamesSmith7
Emerging Contributor
I am using a SearchCursor to return a value from a selected attribute field.  But, the cursor does not work or create an error.  The ValueGain filed is a Long field type that is has not been set to a string.  I am only using the SearchCursor to return a value in the tool dialog window.

arcpy.SelectLayerByAttribute_management(lyr, "NEW_SELECTION", whereClause) df.extent = lyr.getSelectedExtent() df.scale = df.scale*1.1 arcpy.AddMessage(str(whereClause))  cursor = arcpy.da.SearchCursor("Land", ["ValueGain"]) for row in cursor:  arcpy.AddMessage(str(ValueGain))  del row  del cursor
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
ArkadiuszMatoszka
Frequent Contributor
Hi,

When using arcpy.da.SearchCursor() each row is returned as list of values, correct call to get attribute value for row would be:
arcpy.SelectLayerByAttribute_management(lyr, "NEW_SELECTION", whereClause)  df.extent = lyr.getSelectedExtent()  df.scale = df.scale*1.1  arcpy.AddMessage(str(whereClause))   cursor = arcpy.da.SearchCursor("Land", ["ValueGain"])  for row in cursor:   arcpy.AddMessage(str(row[0])) # row[0] = first element of list that represents row  del row  del cursor


In case from your code:
arcpy.AddMessage(str(ValueGain))

ValueGain is unassigned variable, so you will get error.
Regards
Arek

View solution in original post

0 Kudos
3 Replies
ArkadiuszMatoszka
Frequent Contributor
Hi,

When using arcpy.da.SearchCursor() each row is returned as list of values, correct call to get attribute value for row would be:
arcpy.SelectLayerByAttribute_management(lyr, "NEW_SELECTION", whereClause)  df.extent = lyr.getSelectedExtent()  df.scale = df.scale*1.1  arcpy.AddMessage(str(whereClause))   cursor = arcpy.da.SearchCursor("Land", ["ValueGain"])  for row in cursor:   arcpy.AddMessage(str(row[0])) # row[0] = first element of list that represents row  del row  del cursor


In case from your code:
arcpy.AddMessage(str(ValueGain))

ValueGain is unassigned variable, so you will get error.
Regards
Arek
0 Kudos
JamesSmith7
Emerging Contributor
Thanks for the quick response.  Your suggestion worked.
0 Kudos
ChrisSnyder
Honored Contributor
each row is returned as list of values


To be more precise, each row is returned as a Python tuple... which is similar to a Python list, but one that can't be rearanged/sorted (aka an imutable list).

In Python:

tupleExample = (1,2,3,4)
listExample = [1,2,3,4]
0 Kudos