0

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.

  • 2
    You are getting something like "FID;Shape;OBJECTID;x;y;z" so fieldList=fieldList.split(";") will convert it to list and off you go – FelixIP May 29 '16 at 21:25
  • I replaced "fieldList = arcpy.GetParameterAsText(1)" with your part " fieldList=fieldList.split(";")", and I still get an error. I also just put your part of code without erasing "fieldList = arcpy.GetParameterAsText(1)", and I still get an error. – Dean7 May 29 '16 at 21:40
  • 1
    Do not replace. Place it after arcpy.GetParameterAsText(1) – FelixIP May 29 '16 at 21:42
  • 1
    Please update your code to show the changes you've made. Also consider arcpy.AddMessage("{0}".format(fieldList)) immediately before the with line, just to show exactly what the field list contains... you can take that out later (if you want) but it's important to know precisely what's being supplied to the cursor creation. – Michael Stimson May 29 '16 at 22:29
  • 1
    list assignment index out of range = you don't have 2 fields in your fieldList - Please check that your second input parameter is populated with 2 fields in your tool (into fieldList = arcpy.GetParameterAsText(1)) – Midavalo May 29 '16 at 23:32
2

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:

  1. Input file as feature class.
  2. Input field with original data (field as input, no multivalue, derived from feature class
  3. Input field for derived data (field as input, no multivalue, derived from feature class)

enter image description here

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:

  1. Input file as feature class
  2. 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)

enter image description here

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. – Midavalo May 30 '16 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 separately – Dean7 May 30 '16 at 0:27
  • 1
    create two input parameters, inputField = arcpy.GetParameterAsText(1) and outputField = arcpy.GetParameterAsText(2), then combine them into fieldList = [inputField, outputField] – Midavalo May 30 '16 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 May 30 '16 at 0:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.