AnsweredAssumed Answered

Python row iteration - how to proceed if no rows?

Question asked by sagunn on Jun 25, 2012
Latest reply on Jun 26, 2012 by sagunn
Hi all,

This is probably a pretty simple question...I am building a script to populate some text elements on my map layout. Using searchcursor and for loops, I am pulling row values from three different tables that are the final output of a model. The problem is, in certain instances, these tables are intentionally blank because of a lack of data for the query. When I run my script against these blank tables, it produces an error:

<type 'exceptions.NameError'>: name 'row' is not defined
Failed to execute (test).

I have tried to do "if row in rows:" before I iterate but this does not help. Does anyone have any suggestions? thank you! Arcgis 10 SP4, Python2.6.5 Here is my code block:
import arcpy, os, sys

#Reference current MXD
mxd = arcpy.mapping.MapDocument("current")

#Reference appropriate data frames
currentDF = arcpy.mapping.ListDataFrames(mxd,"Layers")[0]

#Reference appropriate tables
surveys = arcpy.mapping.ListTableViews(mxd, "Surveys", currentDF)[0]
overlap = arcpy.mapping.ListTableViews(mxd, "OriginalSurvey", currentDF)[0]
sites = arcpy.mapping.ListTableViews(mxd, "Sites", currentDF)[0]

#Reference layout elements by calling ListLayoutElements only once - get better performance
for elm in arcpy.mapping.ListLayoutElements(mxd):
  if elm.name =="text1col1": text1col1 = elm
  if elm.name =="text1col2": text1col2 = elm
  if elm.name =="text1col3": text1col3 = elm
  if elm.name =="text2col1": text2col1 = elm
  if elm.name =="text3col1": text3col1 = elm

#Clear all table text values
text1col1.text = " "
text1col2.text = " "
text1col3.text = " "
text2col1.text = " "
text3col1.text = " "

#I know I probably don't need this:
surveytable = surveys
overlaptable = overlap
sitetable = sites

#SURVEYS TABLE:
rows = arcpy.SearchCursor(surveytable, "", "", "NMCRIS_NUM; SURVEY_ACRES; TOTAL_ACRES", "NMCRIS_NUM A")

if row in rows:
  for row in rows:
    text1col1.text = text1col1.text + row.getValue("NMCRIS_NUM") + "\n"
    text1col2.text = text1col2.text + row.getValue("SURVEY_ACRES") + "\n"

  text1col3.text = text1col3.text + row.getValue("TOTAL_ACRES") + "\n"
if row:
  del row
if rows:
  del rows
 
#OVERLAP TABLE:
rows = arcpy.SearchCursor(overlaptable, "", "", "TOTAL", "")
if row in rows:
  for row in rows:
    text2col1.text = text2col1.text + row.getValue("TOTAL") + " acres\n"
if row:
  del row
if rows:
  del rows
 
#SITES TABLE:
rows = arcpy.SearchCursor(sitetable, "", "", "ARMSARCHID", "")
if row in rows:
  for row in rows:
    text3col1.text = text3col1.text + "LA " + row.getValue("ARMSARCHID") + "\n"
if row:
  del row
if rows:
  del rows

Outcomes