Hello!
I've been working on a script that will take a group of manually selected features and utilize UpdateCursor to update a field based on three other columns. As of right now, I'm programming it so that messages appear to provide proof of concept. The goal of this is to automate what was a field calculation process into a python add in button.
So, I know that the correct layer is being selected and it will print out the said selected layer's OBJECTID.
However, as soon as I get into updating the cursor, I'll receive an error message saying:
line 29, in onClick
with arcpy.da.UpdateCursor(sample,['block', 'sub_code', 'blockanno']) as blockanno:
RuntimeError: 'in_table' is not a table or a featureclassAny suggestions on how to fix this or get around the error in order to run the nested if statements?
Here is my current script:
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
mxd = arcpy.mapping.MapDocument('current')
df = mxd.activeDataFrame
Fields = ["block", "sub_code", "blockanno"]
lyr = arcpy.mapping.ListLayers(mxd)[1]
pythonaddins.MessageBox("layer is {}".format(lyr), "Message Box", 0)
selected = ''
sample = lyr.getSelectionSet()
for row in sample:
selected = selected + ', ' + str(row)
pythonaddins.MessageBox("selected =" + str(selected), "Message Box", 0)
with arcpy.da.UpdateCursor(sample,['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][0:3].lstrip('0'))
elif row[0].isalpha():
if row[0][0:2] == '00':
row[2] = str(row[0][0:].strip('0'))
# pythonaddins.MessageBox("blockanno is {}".format(row[2]), "Message Box", 0)
blockanno.updateRow(row)