Select to view content in your preferred language

Using lists in arcpy.UpdateCursor function

2602
3
07-01-2013 12:48 PM
kg76
by
Regular Contributor
I keep getting the following error and can't figure out why... I'm new to Python and I'm trying to figure out how to iterate through all feature classes within a geodatabase (because they all have the same attributes) and remove old data from certain fields if new data exists in another field for the same record.

Traceback (most recent call last):
  File "...", line 31, in <module>
    with arcpy.UpdateCursor(fc,fields) as cursor:
AttributeError: __exit__


And here's my script...

#This script is designed to remove legacy accuracy data from a file
#geodatabase if updated accuracy data exists. Legacy accuracy data are
#stored in each feature class under the following field names: B, C,
#D, E, F, G, H, I, J, K, and L. Current accuracy data is stored in a
#field called A. The script evaluates the value in the A field, if the
#value is greater than zero and not null the data in the legacy fields
#for that record need to be set to zero or Null if it is a string
#field.

import arcpy

#Define a workspace

arcpy.env.workspace = r"C:\Example.gdb"

#Create a list of feature classes

fcList = arcpy.ListFeatureClasses()

#Assign variable fields

fields =("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L")

#Loop through each feature class and set to zero or Null the values in
#the legacy accuracy fields (B-L) if data exists in the A field for
#that record

for fc in fcList:
    with arcpy.UpdateCursor(fc,fields) as cursor:
        for row in cursor:
            if (row[0] > 0 and row[0] <> "<Null>"):
                row[1] = 0
                row[2] = 0
                row[3] = 0
                row[4] = 0
                row[5] = 0
                row[6] = 0
                row[7] = "<Null>"
                row[8] = "<Null>"
                row[9] = "<Null>"
                row[10] = "<Null>"
                row[11] = "<Null>"
        cursor.updateRow(row)



Any direction would be appreciated! Thanks.

Kerry
Tags (2)
0 Kudos
3 Replies
JasonScheirer
Esri Alum
If you want to refer to rows as lists like that, you likely want to be using arcpy.da.UpdateCursor, not arcpy.UpdateCursor.
0 Kudos
StacyRendall1
Frequent Contributor
In addition to using arcpy.da.UpdateCursor, as Jason said, you will also need to tab cursor.updateRow(row) across, so that it is within the for loop, meaning that it happens once per row, i.e.:
for fc in fcList:
    with arcpy.UpdateCursor(fc,fields) as cursor:
        for row in cursor:
            if (row[0] > 0 and row[0] <> "<Null>"):
                #...
            cursor.updateRow(row)
0 Kudos
RhettZufelt
MVP Notable Contributor


And here's my script... 

#This script is designed to remove legacy accuracy data from a file
#geodatabase if updated accuracy data exists. Legacy accuracy data are
#stored in each feature class under the following field names: B, C,
#D, E, F, G, H, I, J, K, and L. Current accuracy data is stored in a
#field called A. The script evaluates the value in the A field, if the
#value is greater than zero and not null the data in the legacy fields
#for that record need to be set to zero or Null if it is a string
#field.

import arcpy

#Define a workspace

arcpy.env.workspace = r"C:\Example.gdb"

#Create a list of feature classes

fcList = arcpy.ListFeatureClasses()

#Assign variable fields

fields = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L"]

#Loop through each feature class and set to zero or Null the values in
#the legacy accuracy fields (B-L) if data exists in the A field for
#that record

for fc in fcList:
    with arcpy.da.UpdateCursor(fc,fields) as cursor:  # the code below is for the da.cursor, not original cursor, so I assumed you meant this one.
        for row in cursor:
            if (row[0] > 0 and row[0] is not None):  # this is how to test for NULL in python
                row[1] = 0
                row[2] = 0
                row[3] = 0
                row[4] = 0
                row[5] = 0
                row[6] = 0
                row[7] = None    
                row[8] = None
                row[9] = None
                row[10] = None
                row[11] = None
            cursor.updateRow(row)  #indent this so it updates the row before next row in cursor



Any direction would be appreciated! Thanks. 

Kerry


Can't really test it now, but might be due to loading the fileds in as tuple rather than a list. Also, python doesn't have if x == Null, need to test for is or is not None.

Of course, all this code assumes that all your fields in the fields list are numeric, otherwise, would need quotes (except for the None's).

Also, this won't perform the update "if data exists for field A", but if there is a numeric value in field A that is greater than 0.

if (str(row[0]) is not None:
if (len(str(row[0])) >= 1: would be a couple possible tests for if A has data (including zeros)

R_
0 Kudos