1

This is a find and replace script I have been working on. I feel I am very close, as the script does everything but actually change the values, leading me to believe there is an operator issue. I have been using == as you can see, but have also tried = when the cursor looks for oldText only to get a syntax error. Any thoughts?

import arcpy

FC = arcpy.GetParameterAsText(0)
targetField = arcpy.GetParameterAsText(1)
oldText = arcpy.GetParameterAsText(2)
replaceText = arcpy.GetParameterAsText(3)


fields = arcpy.ListFields(FC)


queryString = queryString = targetField + ' = ' + oldText
print queryString
arcpy.AddMessage(queryString)


try:

    with arcpy.da.UpdateCursor(FC, (targetField), queryString) as cursor:
        for field in fields:
            if field.name == targetField and field.type == 'String':
                for row in cursor:
                    if row[0] == str(oldText):
                        row[0] = str(replaceText)
                    cursor.updateRow(row)

        elif field.name == targetField and field.type == 'Single':
             for row in cursor:
                if row[0] == float(oldText):
                    row[0] = float(replaceText)
                    cursor.updateRow(row)

        elif field.name == targetField and field.type == 'Integer':
             for row in cursor:
                if row[0] == int(oldText):
                    row[0] = int(replaceText)
                    cursor.updateRow(row)




        arcpy.AddMessage("String where " + oldText + " found and replaced with " + replaceText)



except:
        arcpy.AddMessage \(arcpy.GetMessages())
        arcpy.AddError("Failed to find and replace where " + queryString)

closed as off-topic by PolyGeo Mar 17 '16 at 3:44

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions seeking help to debug/write/improve code must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Providing a clear problem statement and evidence of a code attempt will help others to help you. See: How to create a Minimal, Complete, and Verifiable example." – PolyGeo
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 3
    I think you will find debugging Python much easier if you dispense with try/except statements while testing, and in any code snippets posted here, because they mask the errors that Python provides to help you. Also, include copious print statements to make sure that your variables are set to what you think they are all the way through. – PolyGeo Mar 17 '16 at 3:42
  • 1
    Indent your elif statements. – Tom Mar 17 '16 at 5:05
  • Ditto what Tom said. Indent your elif statements. Also, you really don't need to test if row[0] == oldText. The cursor via queryString is only pulling rows where the oldText condition is met, so trust your cursor to do the filtering. If the cursor is not pulling the desired rows, then the problem is in your original SQL statement. See my answer to your other question (gis.stackexchange.com/q/184754/56700) to troubleshoot possible issues with the SQL statement. – Priscilla Mar 18 '16 at 19:35

Browse other questions tagged or ask your own question.