need help with UpdateCursor and .updateRow(row)!!!

1578
5
05-21-2013 07:35 AM
AndrewWickham
New Contributor
Hi all,

I am studying python scripting for geoprocessing and am having trouble with a update cursor loop.

"Property_Layer" is a feature layer and I want to narrow the arcpy.UpdateCursor to empty "LOCALITY" fields. I then want to use the for loop to set the "LOCALITY" field to the variable "value".

my code stalls at the rows.updatRow(row) line. I'm not sure I can use a feature layer with the UpdateCursor....!?

I am new to this so any help would be greatly appreciated! 

    rows = arcpy.UpdateCursor ("Property_Layer",'"LOCALITY" = ' + "'" + "'")                                                         

    row = rows.next()
        
        for row in rows:           
            row.setValue ("LOCALITY", value)           
            rows.updateRow(row)
           
            del row, rows   #delete cursors
Tags (2)
0 Kudos
5 Replies
AndrewWickham
New Contributor
P.S the indenting on my code is correct, not like what is shown in my first post.

thanks!

Andrew
0 Kudos
ChrisSnyder
Regular Contributor III
I think it would look more like this:

value = "CAT"
rows = arcpy.UpdateCursor ("Property_Layer","LOCALITY = '' OR LOCALITY IS NULL") #blank string or Nulls
for row in rows: 
   row.setValue("LOCALITY", value) #assuming this is a text field
   rows.updateRow(row)
del row, rows
0 Kudos
ThaiTruong
Occasional Contributor II
Here is the correct syntax to create an update cursor to help you started

import arcpy

def UpdateField(value):
    # Create update cursor for your feature class
    # Update the correct path to your feature layer
    rows = arcpy.UpdateCursor(r"C:\Temp\test.gdb\Property_Layer")

    # Update/calculate field to the variable "value"
    for row in rows:
        # Update "LOCALITY" Field
        row.LOCALITY= value
        rows.updateRow(row)
    # Delete cursor and row objects to remove locks on the data
    del row
    del rows


Then you can call this UpdateField() function and pass a variable with a value that you want to calculate the field to.
0 Kudos
T__WayneWhitley
Frequent Contributor
I think Chris meant to indent the line for updateRow...
0 Kudos
ChrisSnyder
Regular Contributor III
Woops - yes - correcting that now.
0 Kudos