I'm trying to get the cursor to iterate through rows and fields looking for blank or null values and change them to 999999 but it's not changing anything.

fc = "C:\Users\\bbrock\Documents\ArcGIS\Ports.shp"

# Create a search cursor 
rows = arcpy.SearchCursor(fc) 

# Create a list of string fields
fields = arcpy.ListFields(fc, "", "String")

for row in rows:
    for field in fields:
        if field.type != "Geometry":
            if row.getValue(field.name) == '':
                row.setValue(field.name, '999999')              
                print "%s: Value = %s" % (field.name, row.getValue(field.name))

            if row.isNull(field.name):
                row.setValue(field.name, '999999')              
                print "%s: Value = %s" % (field.name, row.getValue(field.name))             
  • 3
    Could you give us a specific question(s) to help you with?
    – Barbarossa
    Mar 7 '14 at 20:57
  • 1
    Accidentally hit the submit button too early, sorry.
    – brock
    Mar 7 '14 at 20:59
  • I immediately tried to edit, but someone beat me to it and I couldn't override.
    – brock
    Mar 7 '14 at 21:05

A SearchCursor is read-only. You want to use an UpdateCursor. Also don't forget to call the cursor's updateRow method after setting a row's values.

See Accessing data using cursors in the help for more information.


You are using arcpy.SearchCursor which is not able to update values. You need to use an arcpy.UpdateCursor. There are a number of other issues with this code but I will just stick to answering your specific question.

  • What issues are you referring to?
    – brock
    Mar 7 '14 at 21:21
  • 2
    It is best practice to utilize a with statement when using cursors. This will make sure that if something goes wrong everything will be disposed of properly (i.e. remove locks, etc.).For example you would want to do this: with arcpy.da.SearchCursor(fc, "*") as searchCursor: for row in searchCursor: print row Additionally if you are running Arc10.1 or higher arcpy.da is recommended as it is a much faster cursor method.
    – GeoSharp
    Mar 7 '14 at 21:34
  • 2
    Also if you are using ArcGIS 10.1 consider using arcpy.da.updatecursor instead of arcpy.updatecursor for increased performance
    – Conor
    Mar 7 '14 at 22:53

Here is a final working copy of my code using with arcpy.da.updateCursor as... as recommended.

fc = "C:/Users/bbrock/Documents/ArcGIS/Default.gdb/Ports_Admin_Join"

fields = arcpy.ListFields(fc, "", "String")

for field in fields:
    with arcpy.da.UpdateCursor(fc, field.name) as rows:
        for row in rows:
            if field.type != "geometry" and field.length > 5:
                if row[0] == None or row[0] == ' ':
                    row[0] = '999999'
                    print "%s: Value = %s" % (field.name, row[0])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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