Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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

3 Answers 3

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
add comment

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 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. –  JamesSLC Mar 7 at 21:34
1  
Very helpful. Thank you. –  brock Mar 7 at 21:39
1  
Also if you are using ArcGIS 10.1 consider using arcpy.da.updatecursor instead of arcpy.updatecursor for increased performance –  Conor Mar 7 at 22:53
add comment
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, 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])
share|improve this answer
add comment

Your Answer

 
discard

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.