mbd7878

Script fails if SearchCursor has no results.

Discussion created by mbd7878 on Dec 1, 2011
Latest reply on Dec 9, 2011 by mbd7878
I need some help tweeking a script.  I am fairly new to python.  I have a script that uses a SearchCursor to get a value in a field, from a selection by attribute, and then print those results to a text element.  However, if my selection query has no results, the script fails.  I would like it to put in "NONE" in the text element if there is no results in the selection query.  I think the solution might be to Count the the selection and if the result is 0.... but not sure how to do this.

# Select Just the Hydric Soils from the Results of the Intersect Query.
arcpy.SelectLayerByAttribute_management(soilssortlayer,"NEW_SELECTION",' "hydclprs" = \'All Hydric\' ')

# Print Results to Hydric Text Element
elAHsoil = arcpy.mapping.ListLayoutElements(mxd5,"TEXT_ELEMENT","AHSOIL")[0]
rows = arcpy.SearchCursor(soilssortlayer)
textValue = ""
for row in rows:
    textValue += row.getValue("musym_1") + "      " + row.getValue("muname") + '\n'
elAHsoil.text = textValue

Outcomes