Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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( == '':
                row.setValue(, '999999')              
                print "%s: Value = %s" % (, row.getValue(

            if row.isNull(
                row.setValue(, '999999')              
                print "%s: Value = %s" % (, row.getValue(             
share|improve this question
Could you give us a specific question(s) to help you with? – Barbarossa Mar 7 '14 at 20:57
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.

share|improve this answer

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.

share|improve this answer
What issues are you referring to? – brock Mar 7 '14 at 21:21
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. – JamesSLC Mar 7 '14 at 21:34
Very helpful. Thank you. – brock Mar 7 '14 at 21:39
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
up vote 0 down vote accepted

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, 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" % (, row[0])
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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