1

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
  • 3
    Could you give us a specific question(s) to help you with?
    – Barbarossa
    Mar 7, 2014 at 20:57
  • 1
    Accidentally hit the submit button too early, sorry.
    – brock
    Mar 7, 2014 at 20:59
  • I immediately tried to edit, but someone beat me to it and I couldn't override.
    – brock
    Mar 7, 2014 at 21:05

3 Answers 3

10

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.

2

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.

3
  • What issues are you referring to?
    – brock
    Mar 7, 2014 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, 2014 at 21:34
  • 3
    Also if you are using ArcGIS 10.1 consider using arcpy.da.updatecursor instead of arcpy.updatecursor for increased performance
    – Conor
    Mar 7, 2014 at 22:53
0

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'
                    rows.updateRow(row)    
                    print "%s: Value = %s" % (field.name, row[0])
1
  • Although this code may work, instantiating a cursor within a loop is terrible for performance. For the line after checking geometry, the idiomatic way to check for singletons is using is, e.g., row[0] is None, and the second part isn't checking for an empty string but a string with a single space.
    – bixb0012
    Feb 19, 2023 at 13:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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