使用ArcPy for ArcGIS Pro替换字段中的整数值?

时间:2021-09-23 23:41:57

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

在ArcGIS Pro中,要使用字段计算器替换字段中的文本值,您可以使用:

!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?

因此,在Long,Short或任何数字字段中,用另一个数字替换特定数字的正确方法是什么?

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

该函数最终将在独立的Python脚本中实现,如:

import arcpy
arcpy.management.CalculateField(r"C:\Shapefile.shp", "FieldName", "!FieldName!.replace(1250, 810)", "PYTHON3", None)

2 个解决方案

#1


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).

据我所知,replace是一个字符串函数(以及例如大写和rstrip),并且不适用于整数字段(因此语法错误)。

You can do this easy with a code block:

您可以使用代码块轻松完成此操作:

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

Then run: replaceInt(!IntegerFieldName!)

然后运行:replaceInt(!IntegerFieldName!)

#2


1  

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

作为替代方案,您可以使用da.UpdateCursor。它就像一个功能更强大,功能更强大的现场计算器:

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:

如果要更改许多值,可以使用字典而不是许多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)

#1


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).

据我所知,replace是一个字符串函数(以及例如大写和rstrip),并且不适用于整数字段(因此语法错误)。

You can do this easy with a code block:

您可以使用代码块轻松完成此操作:

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

Then run: replaceInt(!IntegerFieldName!)

然后运行:replaceInt(!IntegerFieldName!)

#2


1  

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

作为替代方案,您可以使用da.UpdateCursor。它就像一个功能更强大,功能更强大的现场计算器:

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:

如果要更改许多值,可以使用字典而不是许多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)