English

Error: 'NoneType' object has no attribute 'replace'

Error Message

Attempts to use the replace() function in a Python script on a string field fail and return the following error:

Error: 'NoneType' object has no attribute 'replace'
Error message

Cause

The error occurs when there are Null values in the selected field. The following image shows an attribute table with a field containing Null values.

Attribute table with Null values

The following code sample shows how the error can be reproduced:
import arcpy

cursor = arcpy.da.UpdateCursor ("[Feature]", "[Field Name]"

for row in cursor:
        row[0] = row[0].replace("%20", " ")
        cursor.updateRow(row)

 

Solution or Workaround

Use the selection clause to avoid executing the replace() function on Null values in the field. The following code sample demonstrates how to do so:

import arcpy

cursor = arcpy.da.UpdateCursor("[Feature]", "[Field Name]")

for row in cursor:
        if row[0] == None:
                row[0] = row[0]

        else:
                row[0] = row[0].replace("%20", " ")
        
        cursor.updateRow (row)
Python script

 

Related Information