<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Python row iteration - how to proceed if no rows? in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/python-row-iteration-how-to-proceed-if-no-rows/m-p/92583#M7242</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi all,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;lt;type 'exceptions.NameError'&amp;gt;: name 'row' is not defined&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Failed to execute (test).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;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):
&amp;nbsp; if elm.name =="text1col1": text1col1 = elm
&amp;nbsp; if elm.name =="text1col2": text1col2 = elm
&amp;nbsp; if elm.name =="text1col3": text1col3 = elm
&amp;nbsp; if elm.name =="text2col1": text2col1 = elm
&amp;nbsp; 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:
&amp;nbsp; for row in rows:
&amp;nbsp;&amp;nbsp;&amp;nbsp; text1col1.text = text1col1.text + row.getValue("NMCRIS_NUM") + "\n"
&amp;nbsp;&amp;nbsp;&amp;nbsp; text1col2.text = text1col2.text + row.getValue("SURVEY_ACRES") + "\n"

&amp;nbsp; text1col3.text = text1col3.text + row.getValue("TOTAL_ACRES") + "\n" 
if row:
&amp;nbsp; del row
if rows:
&amp;nbsp; del rows
&amp;nbsp; 
#OVERLAP TABLE:
rows = arcpy.SearchCursor(overlaptable, "", "", "TOTAL", "")
if row in rows:
&amp;nbsp; for row in rows:
&amp;nbsp;&amp;nbsp;&amp;nbsp; text2col1.text = text2col1.text + row.getValue("TOTAL") + " acres\n"
if row:
&amp;nbsp; del row
if rows:
&amp;nbsp; del rows
&amp;nbsp; 
#SITES TABLE:
rows = arcpy.SearchCursor(sitetable, "", "", "ARMSARCHID", "")
if row in rows:
&amp;nbsp; for row in rows:
&amp;nbsp;&amp;nbsp;&amp;nbsp; text3col1.text = text3col1.text + "LA " + row.getValue("ARMSARCHID") + "\n"
if row:
&amp;nbsp; del row
if rows:
&amp;nbsp; del rows&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sun, 12 Dec 2021 16:00:39 GMT</pubDate>
    <dc:creator>ScottGunn</dc:creator>
    <dc:date>2021-12-12T16:00:39Z</dc:date>
    <item>
      <title>Python row iteration - how to proceed if no rows?</title>
      <link>https://community.esri.com/t5/python-questions/python-row-iteration-how-to-proceed-if-no-rows/m-p/92583#M7242</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi all,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;lt;type 'exceptions.NameError'&amp;gt;: name 'row' is not defined&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Failed to execute (test).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;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):
&amp;nbsp; if elm.name =="text1col1": text1col1 = elm
&amp;nbsp; if elm.name =="text1col2": text1col2 = elm
&amp;nbsp; if elm.name =="text1col3": text1col3 = elm
&amp;nbsp; if elm.name =="text2col1": text2col1 = elm
&amp;nbsp; 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:
&amp;nbsp; for row in rows:
&amp;nbsp;&amp;nbsp;&amp;nbsp; text1col1.text = text1col1.text + row.getValue("NMCRIS_NUM") + "\n"
&amp;nbsp;&amp;nbsp;&amp;nbsp; text1col2.text = text1col2.text + row.getValue("SURVEY_ACRES") + "\n"

&amp;nbsp; text1col3.text = text1col3.text + row.getValue("TOTAL_ACRES") + "\n" 
if row:
&amp;nbsp; del row
if rows:
&amp;nbsp; del rows
&amp;nbsp; 
#OVERLAP TABLE:
rows = arcpy.SearchCursor(overlaptable, "", "", "TOTAL", "")
if row in rows:
&amp;nbsp; for row in rows:
&amp;nbsp;&amp;nbsp;&amp;nbsp; text2col1.text = text2col1.text + row.getValue("TOTAL") + " acres\n"
if row:
&amp;nbsp; del row
if rows:
&amp;nbsp; del rows
&amp;nbsp; 
#SITES TABLE:
rows = arcpy.SearchCursor(sitetable, "", "", "ARMSARCHID", "")
if row in rows:
&amp;nbsp; for row in rows:
&amp;nbsp;&amp;nbsp;&amp;nbsp; text3col1.text = text3col1.text + "LA " + row.getValue("ARMSARCHID") + "\n"
if row:
&amp;nbsp; del row
if rows:
&amp;nbsp; del rows&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 16:00:39 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-row-iteration-how-to-proceed-if-no-rows/m-p/92583#M7242</guid>
      <dc:creator>ScottGunn</dc:creator>
      <dc:date>2021-12-12T16:00:39Z</dc:date>
    </item>
    <item>
      <title>Re: Python row iteration - how to proceed if no rows?</title>
      <link>https://community.esri.com/t5/python-questions/python-row-iteration-how-to-proceed-if-no-rows/m-p/92584#M7243</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Just do &lt;/SPAN&gt;&lt;SPAN style="font-family:Courier New;"&gt;row = None&lt;/SPAN&gt;&lt;SPAN&gt; at the beginning (after your &lt;/SPAN&gt;&lt;SPAN style="font-family:Courier New;"&gt;rows=&lt;/SPAN&gt;&lt;SPAN&gt; line), then &lt;/SPAN&gt;&lt;SPAN style="font-family:Courier New;"&gt;del row&lt;/SPAN&gt;&lt;SPAN&gt; will always succeed.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 25 Jun 2012 22:26:21 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-row-iteration-how-to-proceed-if-no-rows/m-p/92584#M7243</guid>
      <dc:creator>JasonScheirer</dc:creator>
      <dc:date>2012-06-25T22:26:21Z</dc:date>
    </item>
    <item>
      <title>Re: Python row iteration - how to proceed if no rows?</title>
      <link>https://community.esri.com/t5/python-questions/python-row-iteration-how-to-proceed-if-no-rows/m-p/92585#M7244</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;OK, your post gave me a couple of ideas...Instead of using a for loop, I switched it to while loops using rows.next().&amp;nbsp; Here's the tail end of my new code, this works!&amp;nbsp; Thanks for the help!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;#SURVEYS: rows = arcpy.SearchCursor(surveys, "", "", "NMCRIS_NUM; SURVEY_ACRES; TOTAL_ACRES", "NMCRIS_NUM A")&amp;nbsp; row = rows.next()&amp;nbsp; while row &amp;lt;&amp;gt; None: &amp;nbsp; text1col1.text = text1col1.text + row.getValue("NMCRIS_NUM") + "\n" &amp;nbsp; text1col2.text = text1col2.text + row.getValue("SURVEY_ACRES") + "\n" &amp;nbsp; text1col3.text = row.getValue("TOTAL_ACRES") &amp;nbsp; row = rows.next()&amp;nbsp; &amp;nbsp;&amp;nbsp; #OVERLAP: rows = arcpy.SearchCursor(overlap, "", "", "TOTAL", "") row = rows.next() while row &amp;lt;&amp;gt; None: &amp;nbsp; text2col1.text = text2col1.text + row.getValue("TOTAL") + " acres\n" &amp;nbsp; row = rows.next() &amp;nbsp;&amp;nbsp; #SITES: rows = arcpy.SearchCursor(sites, "", "", "ARMSARCHID", "ARMSARCHID A") row = rows.next() while row &amp;lt;&amp;gt; None: &amp;nbsp; text3col1.text = text3col1.text + "LA " + row.getValue("ARMSARCHID") + "\n" &amp;nbsp; row = rows.next()&amp;nbsp;&amp;nbsp; if row: &amp;nbsp; del row if rows: &amp;nbsp; del rows &amp;nbsp;&amp;nbsp; #END&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 26 Jun 2012 13:56:57 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-row-iteration-how-to-proceed-if-no-rows/m-p/92585#M7244</guid>
      <dc:creator>ScottGunn</dc:creator>
      <dc:date>2012-06-26T13:56:57Z</dc:date>
    </item>
  </channel>
</rss>

