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.
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.
-
Have you recreated the toolbox itself in 10.3 as well?Brian– Brian2016年09月21日 18:33:27 +00:00Commented 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.Dean7– Dean72016年09月21日 18:36:50 +00:00Commented Sep 21, 2016 at 18:36
-
4What error are you getting?Tom– Tom2016年09月21日 18:38:10 +00:00Commented Sep 21, 2016 at 18:38
-
5Remove 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.Midavalo– Midavalo ♦2016年09月21日 18:40:55 +00:00Commented Sep 21, 2016 at 18:40
-
Among other changes I made, that also worked. Will post correct answer.Dean7– Dean72016年09月21日 19:29:18 +00:00Commented Sep 21, 2016 at 19:29
2 Answers 2
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 berow.setValue()
cur.UpdateRow()
should becur.updateRow()
cur.Next()
should becur.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.
-
1I did everything you said, and now it works great.Dean7– Dean72016年09月21日 19:41:20 +00:00Commented Sep 21, 2016 at 19:41
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
-
1Once 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.2016年09月21日 19:48:58 +00:00Commented 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.Dean7– Dean72016年09月21日 19:53:19 +00:00Commented Sep 21, 2016 at 19:53
-
2Your 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/…user2856– user28562016年09月23日 09:21:54 +00:00Commented Sep 23, 2016 at 9:21