Update Cursor not updating field with value

2351
3
Jump to solution
02-14-2012 07:39 AM
RoyHewitt
New Contributor III
I'm writing a script for field staff that clips all wetlands in a watershed, calculates area (hectares) and multiples by a sampling factor to see how many hectares need to be sample

Here is a sample of the update cursor part of the code:
         rows = arcpy.UpdateCursor(newWetland) row = rows.next()  while row:      if row.getValue(wetlandTypeField) == 'Estuarine and Marine Wetland':           totalHectares = row.getValue(areaField)           sampleArea = (totalHectares * .25) # Sample 25% of this type           row.setValue(sampleField, sampleArea)           rows.updateRow(row)           row = rows.next()      elif row.getValue(wetlandTypeField) == 'Freshwater Emergent Wetland':           totalHectares = row.getValue(areaField)           sampleArea = (totalHectares * .25) # Sample 25% of this type           row.setValue(sampleField, sampleArea)           rows.updateRow(row)           row = rows.next()


There are several more elif blocks for each wetland type in the dataset.

When I type in the python window:
print sampleArea

I get the correct area, however, it does not update the row.  The help files indicate that the second parameter of the row.setValue() method is supposed to be an object.  Could that be the problem?

Any help would be appreciated.
Roy
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
DanPatterson_Retired
MVP Emeritus
have you tried to use the "for row in rows:" syntax as shown in the online help for updaterow?
http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//002z0000001q000000

View solution in original post

0 Kudos
3 Replies
DanPatterson_Retired
MVP Emeritus
have you tried to use the "for row in rows:" syntax as shown in the online help for updaterow?
http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//002z0000001q000000
0 Kudos
RoyHewitt
New Contributor III
I rewrote the code to use the for loop.  I got the same problem at first (all null values in the attribute table), I went into field calculator and closed it without entering any code and VOILA, the correct values were in the table.

I'm not sure if they would have been had I gone into the field calculator after running the while loop or not, but success none the less.

Is there any "refresh" method that would fix this??

Thanks again!
0 Kudos
DanPatterson_Retired
MVP Emeritus
The table doesn't get refreshed.  Run scripts etc, then open the table, don't expect updates unless you are using the field calculator directly.
0 Kudos