I am trying to convert python script to tool. Tool would update row values based on other row values. For example, if in row One values are Water, Rivers, Streams, in row Two, updated values would be "Yes", "No", "Maybe".
Using this script in this post Using UpdateCursor to update a field based on other field values, I have to managed to write my code to update multiple rows.
This is my working code, for python and one row:
import arcpy
fc = r'D:\D_Temp\Folder\Data.shp'
with arcpy.da.UpdateCursor(fc, ["Description", "New"]) as cursor:
for row in cursor:
if row[0] == "Water":
row[1] = "Yes"
cursor.updateRow(row)
if row[0] == "Rivers":
row[1] = "No"
cursor.updateRow(row)
if row[0] == "Streams":
row[1] = "Maybe"
cursor.updateRow(row)
EDITED (with part of the code FelixIP suggested):
When I'm trying to convert the script as python tool script, I do it like this. And I also put correct parameters (feature class as input data, field for original data, and field for derived data):
import arcpy
fc = arcpy.GetParameterAsText(0)
fieldList = arcpy.GetParameterAsText(1)
fieldList = fieldList.split(";")
with arcpy.da.UpdateCursor(fc, fieldList) as cursor:
for row in cursor:
if row[0] == "Water":
row[1] = "Yes"
cursor.updateRow(row)
if row[0] == "Rivers":
row[1] = "No"
cursor.updateRow(row)
if row[0] == "Streams":
row[1] = "Maybe"
cursor.updateRow(row)
but no matter the parameters i define, I end up with this error:
Traceback (most recent call last): File "C:\Users\Name\Desktop\Script.py", line 7, in row1 = "Yes" IndexError: list assignment index out of range. Failed to execute (NewScript).
I think I am missing some sort of list for rows, but I am not sure.
1 Answer 1
As FelixIP pointed out, first I had to add some part of the code, which was missing. Second, there was a mistake with input parameters, but Midavalo sorted it out. At first, I had 3 parameters:
- Input file as feature class.
- Input field with original data (field as input, no multivalue, derived from feature class
- Input field for derived data (field as input, no multivalue, derived from feature class)
Mistake was that there was no need to have two input fields as parameters, just one input parameter (but as multivalue), along with input data. So correct way was to have:
- Input file as feature class
- Input fields for both original data, and for derived data (both fields as input, derived from feature class, but as multivalue, so the tool could be executed)
And this is the working code:
import arcpy
fc = arcpy.GetParameterAsText(0)
fieldList = arcpy.GetParameterAsText(1)
fieldList = fieldList.split(";")
with arcpy.da.UpdateCursor(fc, fieldList) as cursor:
for row in cursor:
if row[0] == "Water":
row[1] = "Yes"
cursor.updateRow(row)
if row[0] == "Rivers":
row[1] = "No"
cursor.updateRow(row)
if row[0] == "Streams":
row[1] = "Maybe"
cursor.updateRow(row)
EDIT:
Although the first code is working, there was no need to make one input parameters as multivalue, there still could be two input fields as two parameters. In that case two list fields must be made (as first picture in this answer).
This is the code (more complete as the first one, as this one is not depended in which order there are fields)
import arcpy
fc = arcpy.GetParameterAsText(0)
inputField = arcpy.GetParameterAsText(1)
outputField = arcpy.GetParameterAsText(2)
fieldList = [inputField, outputField]
with arcpy.da.UpdateCursor(fc, fieldList) as cursor:
for row in cursor:
if row[0] == "Water":
row[1] = "Yes"
cursor.updateRow(row)
if row[0] == "Rivers":
row[1] = "No"
cursor.updateRow(row)
if row[0] == "Streams":
row[1] = "Maybe"
cursor.updateRow(row)
-
The risk in having one parameter for two fields is if your fields are somehow selected in a different order. Safer to have two single-value parameters and create your
fieldList
from these two inputs.2016年05月30日 00:22:55 +00:00Commented May 30, 2016 at 0:22 -
Yes, I tried my tool and there is a problem you pointed now. But tool worked. But i do not know where to create fieldList from these two input fields if they are selected separatelyDean7– Dean72016年05月30日 00:27:31 +00:00Commented May 30, 2016 at 0:27
-
1create two input parameters,
inputField = arcpy.GetParameterAsText(1)
andoutputField = arcpy.GetParameterAsText(2)
, then combine them intofieldList = [inputField, outputField]
2016年05月30日 00:29:17 +00:00Commented May 30, 2016 at 0:29 -
Of course! So simple! I undrestood now, where the mistake was in a first place. In creating parameters for fields! and with this code, there was no need for part that FelixIP suggested.Dean7– Dean72016年05月30日 00:35:57 +00:00Commented May 30, 2016 at 0:35
list assignment index out of range
= you don't have 2 fields in yourfieldList
- Please check that your second input parameter is populated with 2 fields in your tool (intofieldList = arcpy.GetParameterAsText(1)
)