Python row iteration - how to proceed if no rows?

3765
2
Jump to solution
06-25-2012 01:48 PM
ScottGunn
New Contributor III
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
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
ScottGunn
New Contributor III
OK, your post gave me a couple of ideas...Instead of using a for loop, I switched it to while loops using rows.next().  Here's the tail end of my new code, this works!  Thanks for the help!

#SURVEYS: rows = arcpy.SearchCursor(surveys, "", "", "NMCRIS_NUM; SURVEY_ACRES; TOTAL_ACRES", "NMCRIS_NUM A")  row = rows.next()  while row <> None:   text1col1.text = text1col1.text + row.getValue("NMCRIS_NUM") + "\n"   text1col2.text = text1col2.text + row.getValue("SURVEY_ACRES") + "\n"   text1col3.text = row.getValue("TOTAL_ACRES")   row = rows.next()     #OVERLAP: rows = arcpy.SearchCursor(overlap, "", "", "TOTAL", "") row = rows.next() while row <> None:   text2col1.text = text2col1.text + row.getValue("TOTAL") + " acres\n"   row = rows.next()    #SITES: rows = arcpy.SearchCursor(sites, "", "", "ARMSARCHID", "ARMSARCHID A") row = rows.next() while row <> None:   text3col1.text = text3col1.text + "LA " + row.getValue("ARMSARCHID") + "\n"   row = rows.next()   if row:   del row if rows:   del rows    #END

View solution in original post

0 Kudos
2 Replies
JasonScheirer
Occasional Contributor III
Just do row = None at the beginning (after your rows= line), then del row will always succeed.
0 Kudos
ScottGunn
New Contributor III
OK, your post gave me a couple of ideas...Instead of using a for loop, I switched it to while loops using rows.next().  Here's the tail end of my new code, this works!  Thanks for the help!

#SURVEYS: rows = arcpy.SearchCursor(surveys, "", "", "NMCRIS_NUM; SURVEY_ACRES; TOTAL_ACRES", "NMCRIS_NUM A")  row = rows.next()  while row <> None:   text1col1.text = text1col1.text + row.getValue("NMCRIS_NUM") + "\n"   text1col2.text = text1col2.text + row.getValue("SURVEY_ACRES") + "\n"   text1col3.text = row.getValue("TOTAL_ACRES")   row = rows.next()     #OVERLAP: rows = arcpy.SearchCursor(overlap, "", "", "TOTAL", "") row = rows.next() while row <> None:   text2col1.text = text2col1.text + row.getValue("TOTAL") + " acres\n"   row = rows.next()    #SITES: rows = arcpy.SearchCursor(sites, "", "", "ARMSARCHID", "ARMSARCHID A") row = rows.next() while row <> None:   text3col1.text = text3col1.text + "LA " + row.getValue("ARMSARCHID") + "\n"   row = rows.next()   if row:   del row if rows:   del rows    #END
0 Kudos