<?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 Running nested if then statements on selected features in a Python addin button in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/running-nested-if-then-statements-on-selected/m-p/1056402#M61083</link>
    <description>&lt;P&gt;Hello!&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm currently trying to create a button within arcmap that runs a set of if-then rules to fill in a blank field within the layer's attribute table. I've been able to do this in the field calculator with no problem, but in order to speed up the process, I would like to be able to run the process in one click rather than manually labeling.&amp;nbsp;&lt;/P&gt;&lt;P&gt;My issue is that I don't know how to get it so that the button takes the already selected features and then proceeds to run the nested statements.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy
import pythonaddins

class BlockAnno(object):
    """Implementation for Test_addin.button (Button)"""
    def __init__(self):
        self.enabled = True
        self.checked = False
    def onClick(self):
		lyr = pythonaddins.GetSelectedTOCLayerOrDataFrame()
        with arcpy.da.UpdateCursor(lyr,["block", "sub_code"]) as blockanno:
			if sub_code != '0000': 
				if sub_code != '0780':
					if block[0:3].isdigit():
						blockanno = str(block[0:3].lstrip('0'))
					elif block[2].isalpha():
						if block[0:2] == '00':
							blockanno = str(block[0:].strip('0'))
		cursor.updateRow()
return(BlockAnno)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm hoping this is feasible, but would appreciate any guidance or advice.&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 10 May 2021 19:50:17 GMT</pubDate>
    <dc:creator>kad37_pns</dc:creator>
    <dc:date>2021-05-10T19:50:17Z</dc:date>
    <item>
      <title>Running nested if then statements on selected features in a Python addin button</title>
      <link>https://community.esri.com/t5/python-questions/running-nested-if-then-statements-on-selected/m-p/1056402#M61083</link>
      <description>&lt;P&gt;Hello!&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm currently trying to create a button within arcmap that runs a set of if-then rules to fill in a blank field within the layer's attribute table. I've been able to do this in the field calculator with no problem, but in order to speed up the process, I would like to be able to run the process in one click rather than manually labeling.&amp;nbsp;&lt;/P&gt;&lt;P&gt;My issue is that I don't know how to get it so that the button takes the already selected features and then proceeds to run the nested statements.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy
import pythonaddins

class BlockAnno(object):
    """Implementation for Test_addin.button (Button)"""
    def __init__(self):
        self.enabled = True
        self.checked = False
    def onClick(self):
		lyr = pythonaddins.GetSelectedTOCLayerOrDataFrame()
        with arcpy.da.UpdateCursor(lyr,["block", "sub_code"]) as blockanno:
			if sub_code != '0000': 
				if sub_code != '0780':
					if block[0:3].isdigit():
						blockanno = str(block[0:3].lstrip('0'))
					elif block[2].isalpha():
						if block[0:2] == '00':
							blockanno = str(block[0:].strip('0'))
		cursor.updateRow()
return(BlockAnno)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm hoping this is feasible, but would appreciate any guidance or advice.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 10 May 2021 19:50:17 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/running-nested-if-then-statements-on-selected/m-p/1056402#M61083</guid>
      <dc:creator>kad37_pns</dc:creator>
      <dc:date>2021-05-10T19:50:17Z</dc:date>
    </item>
    <item>
      <title>Re: Running nested if then statements on selected features in a Python addin button</title>
      <link>https://community.esri.com/t5/python-questions/running-nested-if-then-statements-on-selected/m-p/1056407#M61084</link>
      <description>&lt;P&gt;Edited to fix indexes.&lt;/P&gt;&lt;P&gt;First look at it, I think you are missing some things for the cursor.&amp;nbsp; It looks like you are missing the iteration part:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;for row in blockanno:
    if row[1] != '0000' and row[1] != '0780':
     ...&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;and&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;cursor.updateRow()&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;should be&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;blockanno.updateRow(row)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You need to include the field that you are updating.&amp;nbsp; As written, blockanno is the cursor object, so include the field that you want to update after the "sub_code" in the list of fields there so its index is row[2].&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;row[2] = str(row[0:].strip('0'))&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hopefully this will get you going.&lt;/P&gt;</description>
      <pubDate>Mon, 10 May 2021 20:13:33 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/running-nested-if-then-statements-on-selected/m-p/1056407#M61084</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2021-05-10T20:13:33Z</dc:date>
    </item>
    <item>
      <title>Re: Running nested if then statements on selected features in a Python addin button</title>
      <link>https://community.esri.com/t5/python-questions/running-nested-if-then-statements-on-selected/m-p/1056409#M61085</link>
      <description>&lt;P&gt;And you need to indent the .updateRow(row) to be within the&amp;nbsp; ' with arcpy.da.UpdateCursor() ' call.&lt;/P&gt;</description>
      <pubDate>Mon, 10 May 2021 20:07:19 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/running-nested-if-then-statements-on-selected/m-p/1056409#M61085</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2021-05-10T20:07:19Z</dc:date>
    </item>
    <item>
      <title>Re: Running nested if then statements on selected features in a Python addin button</title>
      <link>https://community.esri.com/t5/python-questions/running-nested-if-then-statements-on-selected/m-p/1057091#M61098</link>
      <description>&lt;P&gt;Thank you! That definitely changed my button from it's missing state. Unfortunately, however, even after the alterations to the script, it will not change those null values from the blockanno column to a product following the nested statements. Any thoughts? I suspect it's stemming from trying to grab the manually grabbed selected features, because if I run that function in the python window of my project, lyr comes up with "none".&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy
import pythonaddins

class ButtonClass1(object):
    """Implementation for Test2_addin.button (Button)"""
    def __init__(self):
        self.enabled = True
        self.checked = False
    def onClick(self):
        ## if editing:
		## elif msg -- put on edit  and then exit
		lyr = pythonaddins.GetSelectedTOCLayerOrDataFrame()
		with arcpy.da.UpdateCursor(lyr,["block", "sub_code", "blockanno"]) as blockanno:
			for row in blockanno:
				if row[1] != '0000' and row[1] != '0780':
					if row[0][0:3].isdigit():
						row[2] = str(row[0:3].lstrip('0'))
					elif row[0].isalpha():
						if row[0][0:2] == '00':
							row[2] = str(row[0:].strip('0'))
			blockanno.updateRow(row)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 12 May 2021 13:38:54 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/running-nested-if-then-statements-on-selected/m-p/1057091#M61098</guid>
      <dc:creator>kad37_pns</dc:creator>
      <dc:date>2021-05-12T13:38:54Z</dc:date>
    </item>
    <item>
      <title>Re: Running nested if then statements on selected features in a Python addin button</title>
      <link>https://community.esri.com/t5/python-questions/running-nested-if-then-statements-on-selected/m-p/1057304#M61101</link>
      <description>&lt;P&gt;Looking at the lines where you are assigning values to row[2], it looks like you are not selecting specific row index to slice. &amp;nbsp;you will need to reference that index position&lt;/P&gt;&lt;LI-CODE lang="python"&gt;row[2] = str(row[0][0:3].lstrip('0'))

and
row[2] = str(row[0][0:].strip('0'))&lt;/LI-CODE&gt;&lt;P&gt;if you wanted to concatenate row index 0 and 1, you'll have to add each row index like&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;row[2] = f'{row[0]}{row[1]}'.lstrip('0')

and 

row[2] = f'{row[0]}{row[1]}'.strip('0')&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 12 May 2021 18:43:15 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/running-nested-if-then-statements-on-selected/m-p/1057304#M61101</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2021-05-12T18:43:15Z</dc:date>
    </item>
  </channel>
</rss>

