ERROR

'NoneType' object has no attribute 'replace'

Last Published: April 25, 2020

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

 

Article ID:000014467

Software:
  • ArcMap
  • ArcGIS Pro

Receive notifications and find solutions for new or common issues

Get summarized answers and video solutions from our new AI chatbot.

Download the Esri Support App

Related Information

Discover more on this topic

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options