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)
Solved! Go to Solution.
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__'
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?
Hi Vishal, I see the same error as you using arcpy.UpdateCursor. Using arcpy.da.UpdateCursor as Joshua recommended fixed it for me.