1

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.

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Sep 21, 2016 at 18:12
5
  • Have you recreated the toolbox itself in 10.3 as well? Commented Sep 21, 2016 at 18:33
  • No, I didn't seem to think of that idea. I did not know that I also should recreate the toolbox, since I have one with the scripts. Commented Sep 21, 2016 at 18:36
  • 4
    What error are you getting? Commented Sep 21, 2016 at 18:38
  • 5
    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. Commented Sep 21, 2016 at 18:40
  • Among other changes I made, that also worked. Will post correct answer. Commented Sep 21, 2016 at 19:29

2 Answers 2

4

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.

answered Sep 21, 2016 at 19:17
1
  • 1
    I did everything you said, and now it works great. Commented Sep 21, 2016 at 19:41
2

So, as Midavalo pointed, I had to remove try/except, and that was only part of solution.

I also had to change indentation for some parts of the code, when I removed try/except. Also I removed some letters from capital to lowercase like Next to next, GetValue to getValue, SetValue to setValue.

I did not have to recreate toolbox as Brian said.

This is working code now:

import arcpy, sys
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()
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:
 if value.upper().find(findStr.upper()) != -1:
 valueReplace = value.replace(findStr, replaceWith)
 row.setValue(field, valueReplace)
 cur.updateRow(row)
 row = cur.next()
arcpy.SetParameterAsText(6, table)
del cur, row
answered Sep 21, 2016 at 19:39
3
  • 1
    Once you have your working script you can put your Try/Except back in (and re-indent the relevant portions of code). The Try/Except can hinder debugging, but once it's working it can be useful in allowing scripts to run without seeing major messages when they're not needed. Commented Sep 21, 2016 at 19:48
  • Thank you for that input. Will surely put it back, and try toreconstruct the code again with try/except part. Commented Sep 21, 2016 at 19:53
  • 2
    Your next step should be to convert from the old arcpy cursors to the new arcpy.da cursors, they are much faster - desktop.arcgis.com/en/arcmap/10.3/analyze/arcpy-data-access/… Commented Sep 23, 2016 at 9:21

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.