1

In ArcGIS Pro, to replace a text value in a field using Field Calculator you can use:

!FieldName!.replace("Old Value", "New Value")

However, this does not work on an integer field. For example:

!IntegerFieldName!.replace(1250, 810)

or

!IntegerFieldName!.replace("1250", "810")

Will both produce syntax errors.

So in a Long, Short, or any numeric field, what is the correct way to replace a specific number with another number?

This function will eventually be implemented in a standalone Python script like:

import arcpy
arcpy.management.CalculateField(r"C:\Shapefile.shp", "FieldName", "!FieldName!.replace(1250, 810)", "PYTHON3", None)
2
  • You could just select by attributes all 1250:s and then Field Calculate to 810
    – Bera
    Commented Sep 17, 2018 at 10:00
  • @BERA I should mention that eventually I'm writing this script to be run outside of ArcGIS Pro. So there'd be no selecting by atributes etc. This would be a pure standalone python code.
    – Theo F
    Commented Sep 17, 2018 at 10:01

2 Answers 2

3

As far as I know, replace is a string function (along with e.g. capitalize and rstrip) and will not work on integer fields (hence the syntax error).

You can do this easy with a code block:

def replaceInt(number):
    if number == 1250:
        return 810
    else:
        return number

Then run: replaceInt(!IntegerFieldName!)

5
  • Yes that was my feeling regarding it being a string function. I'm still a bit of an arcpy noob. So what would the code be if I wanted to replace 1250 with 810? Do I need to type in the Code Block area?
    – Theo F
    Commented Sep 17, 2018 at 9:59
  • I've edited in a suggestion in the answer above. If there are several replaces they can be added in as elif statements in the same code.
    – HavardMoe
    Commented Sep 17, 2018 at 10:10
  • thanks. that works fine in Field Calculator within the application. However, I'd like to implement this as part of a standalone Python script (see my edited original question). Do you know how I could do this?
    – Theo F
    Commented Sep 17, 2018 at 10:12
  • 1
    You can use field calculator in arcpy, see here (example 3 shows use with a code block): pro.arcgis.com/en/pro-app/tool-reference/data-management/…
    – HavardMoe
    Commented Sep 17, 2018 at 10:24
  • Although... in a standalone script it would be more efficient to use an UpdataCursor, as @BERA mentions above
    – HavardMoe
    Commented Sep 17, 2018 at 10:34
2

As an alternative you can use the da.UpdateCursor. It is like a more versatile and powerful field calculator:

import arcpy

fc = r'C:\data.gdb\featureclass123' #Change to match your data
field_to_update = 'IntegerFieldName' #Change to match your data

with arcpy.da.UpdateCursor(fc,field_to_update) as cursor:
    for row in cursor:
        if row[0] == 1250:
            row[0] = 810
            cursor.updateRow(row)

If you have many values to change, you can use a dictionary instead of many if-elif-else:

integer_dictionary = {1250:810, 1251:900, 1300:1000} #Add keys and values here
with arcpy.da.UpdateCursor(fc,field_to_update) as cursor:
    for row in cursor:
        if row[0] in integer_dictionary:
            row[0] = integer_dictionary[row[0]]
            cursor.updateRow(row)

Your Answer

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

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