Select to view content in your preferred language

AttributeError: __exit__

5823
12
Jump to solution
03-14-2018 07:05 AM
VishalShah2
Occasional Contributor II

Hello,

So I am new to working with cursors and need a little help with my code as I am getting an AttributeError: __exit__ when i run my script. Here's what my code looks like:

inTable = r'Layer\Fiber'
routeMilesField = "RouteMiles"
strandsField = "cableSize"
milesField = "FiberMiles"
fields = [strandsField, milesField, routeMilesField]


with arcpy.UpdateCursor(fiberFeatures, fields) as cursor:
    for row in cursor:
        if None in row[:-1]:
            arcpy.AddMessage("Insufficient Strand Data!")
            break
        else:
            row[2] = row[0] * row[1]
            cursor.updateRow(row)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Any help is greatly appreciated!

Edit:

Revised script based on this article: cursor - AttributeError: __exit__ when trying to use arcpy.UpdateCursor - Geographic Information Sys... 

cursor = arcpy.UpdateCursor(fiberFeatures, fields)
for row in cursor:
    if None in row[:-1]:
        arcpy.AddMessage("Insufficient Strand Data")
        break
    else:
        row[2] = row[0] * row[1]
        cursor.updateRow(row)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

This returns a TypeError: 'Row' object has no attribute '__getitem__' error. I'm new to cursors and what not and definitely need help with this code.

Edit 2: Solution based on Joshua's feedback:

with arcpy.da.UpdateCursor(fiberFeatures, fields) as cursor:
    for row in cursor:
        if None in row[:-1]:
            arcpy.AddError("Insufficient Strand Data!")
            break
        else:
            row[2] = row[0] * row[1]
            cursor.updateRow(row)
0 Kudos
12 Replies
VishalShah2
Occasional Contributor II
cursor = arcpy.UpdateCursor(fiberFeatures, fields)
for row in cursor:
    if not (None in row[:-1]):
        row[2] = row[0] * row[1]
        cursor.updateRow(row)

Tried this and got TypeError: 'Row' object has no attribute '__getitem__'

0 Kudos
VishalShah2
Occasional Contributor II

I found this article by Charles Nagy Python error: AttributeError: __exit__ | Charles Nagy but my code has a few difference and I do not know how to re engineer my code to go along the lines of what Charles has to eliminate the error. Any help on this?

0 Kudos
JustinPierre
New Contributor II

Hi Vishal, I see the same error as you using arcpy.UpdateCursor. Using arcpy.da.UpdateCursor as Joshua recommended fixed it for me.