[GIS] Converting Python script tools from arcgisscripting to ArcPy

arcpypython-script-tool

I have found some toolbox with python scripts from ArcGIS 9.3, and I am trying to edit that script to work in ArcGIS 10.3.

This is the picture of the tool dialog.

enter image description here

And this is the code for older version.

import arcgisscripting, sys, traceback
gp = arcgisscripting.create()

# User-defined parametrs
table = gp.GetParameterAsText(0) #Table: Feature Layer
field = gp.GetParameterAsText(1) #Field: Field
whereClause = gp.GetParameterAsText(2)
findStr = gp.GetParameterAsText(3) #Find:  String
replaceWith = gp.GetParameterAsText(4) #Replace with:  String
matchCase = gp.GetParameterAsText(5) # Match Case:  Boolean

# Check for sql query
if whereClause == "#":
    cur = gp.UpdateCursor(table)
else:
    cur = gp.UpdateCursor(table, whereClause)

# Update cursor
row = cur.Next()

try:
    while row:
        # Get field value
        value = row.GetValue(field)
        # Find case sensitive string
        if matchCase == "true":
            if value.find(findStr) != -1:
                # Replace string
                valueReplace = value.replace(findStr, replaceWith)
                # Insert new string into field
                row.SetValue(field, valueReplace)
                cur.UpdateRow(row)
              else:
                  gp.AddWarning(findStr + " not found.")
        else:  # Find string (not case sensitive)
            if value.upper().find(findStr.upper()) != -1:
                # Replace string
                valueReplace = value.replace(findStr, replaceWith)
                # Insert new string into field
                row.SetValue(field, valueReplace)
                cur.UpdateRow(row)
              else:
                  gp.AddWarning(findStr + " not found.")
        # Point to next feature
        row = cur.Next()

    # Set parameter
    gp.SetParameterAsText(6, table)

except arcgisscripting.ExecuteError:
    # Get the geoprocessing error messages
    msgs = gp.GetMessage(0)
    msgs += gp.GetMessages(2)

    # Return gp error messages for use with a script tool
    gp.AddError(msgs)

    # Print gp error messages for use in Python/PythonWin
    print msgs

except:
    # Get the traceback object
    tb = sys.exc_info()[2]
    tbinfo = traceback.format_tb(tb)[0]

    # Concatenate information together concerning the error into a     message string
    pymsg = tbinfo + "\n" + str(sys.exc_type)+ ": " + str(sys.exc_value)

    # Return python error messages for use with a script tool
    gp.AddError(pymsg)

    # Print Python error messages for use in Python/PythonWin
    print pymsg

# Delete reference to curosr object (unlock data)
del cur, row

I try to convert some of main and obvious parts of the scripts, such as to convert arcgisscripting to arcpy, and gp to arcpy. And I also deleted some informative parts of the code, but I am still missing something. This is non working arcpy code:

import arcpy, sys, traceback
table = arcpy.GetParameterAsText(0)
field = arcpy.GetParameterAsText(1)
whereClause = arcpy.GetParameterAsText(2)
findStr = arcpy.GetParameterAsText(3)
replaceWith = arcpy.GetParameterAsText(4)
matchCase = arcpy.GetParameterAsText(5)

if whereClause == "#":
    cur = arcpy.UpdateCursor(table)
else:
    cur = arcpy.UpdateCursor(table, whereClause)

row = cur.Next()

try:
    while row:
        value = row.GetValue(field)
        if matchCase == "true":
            if value.find(findStr) != -1:
                valueReplace = value.replace(findStr, replaceWith)
                row.SetValue(field, valueReplace)
                cur.UpdateRow(row)
              else:
                  arcpy.AddWarning(findStr + " not found.")
        else:  # Find string (not case sensitive)
            if value.upper().find(findStr.upper()) != -1:
                valueReplace = value.replace(findStr, replaceWith)
                row.SetValue(field, valueReplace)
                cur.UpdateRow(row)
              else:
                  arcpy.AddWarning(findStr + " not found.")
        row = cur.Next()

    arcpy.SetParameterAsText(6, table)

except arcpy.ExecuteError:
    msgs = arcpy.GetMessage(0)
    msgs += arcpy.GetMessages(2)

    # Return arcpy error messages for use with a script tool
    arcpy.AddError(msgs)

    # Print arcpy error messages for use in Python/PythonWin
    print msgs

except:
    # Get the traceback object
    tb = sys.exc_info()[2]
    tbinfo = traceback.format_tb(tb)[0]

    # Concatenate information together concerning the error into a message string
    pymsg = tbinfo + "\n" + str(sys.exc_type)+ ": " + str(sys.exc_value)

    # Return python error messages for use with a script tool
    arcpy.AddError(pymsg)

    # Print Python error messages for use in Python/PythonWin
    print pymsg

# Delete reference to curosr object (unlock data)
del cur, row

I have few codes like this one, and I would like to know, what to change (in this code, and other ones) and make scripts to work in arcmap 10.3.

Best Answer

Remove your Try/Except so that you get all errors in full and just run it again. It should tell you the line that's not working and you'll know where to focus next.

Double-check your capitalization - arcpy is a bit pickier with caps. The following in your code need caps changed:

  • row.SetValue() should be row.setValue()
  • cur.UpdateRow() should be cur.updateRow()
  • cur.Next() should be cur.next()

For correct case (and correct usage) check the online help for each function.

Once you get everything working you may also want to check out some of the new or improved functions. From your script above I would suggest investigating arcpy.da.UpdateCursor() to replace arcpy.UpdateCursor() as they are much more efficient when run, but are written quite different.

A quick search on google found this useful blog about converting your scripts.