I tried this code in ArcGIS Pro.
I am getting the code to work great in command line. However, trying to script a tool out it in ArcGIS Pro.
Getting an error:
File "", line 13, in AttributeError: 'str' object has no attribute 'extend'.
Here is the code:
import arcpy
#input data and fields to find maximum
table = arcpy.GetParameterAsText(0)
fields = arcpy.GetParameterAsText(1)
n = len(fields)
# Add fields to input table to store maximum and field name
maxfield = arcpy.GetParameterAsText(2)
maxname = arcpy.GetParameterAsText(3)
arcpy.AddField_management(table, maxfield, "DOUBLE")
arcpy.AddField_management(table, maxname, "TEXT"
fields2 = fields[:] # shallow copy
fields2.extend([maxfield, maxname])
with arcpy.da.UpdateCursor(table, fields2) as cursor:
for row in cursor:
# Look at the first n values
check = row[:n]
maxval = max(check)
# Get the index position of the maxval and use it slice into field list
# which gives us the field name
# only update the last two rows
row[-2:] = maxval, fields[check.index(maxval)]
cursor.updateRow(row)
1 Answer 1
You are using GetParamterAsText, so the input fields variable is a string. If you print the fields variable using arcpy.AddMessage you will see how the GetParameterAsText handles the multiple field inputs as text. Convert the input string to a list.
fields = arcpy.GetParameterAsText(1).replace("'", "").split(";")