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.
Instead of investing in fixing code that uses the old ArcPy cursors, I recommend spending a few minutes to read up on the not-so-new ArcPy Data Access cursors. They are much more efficient, and a bit more Pythonic, than the original Python cursors. UpdateCursor—Help | ArcGIS Desktop
You are missing a quote at the end of the first line. See if that makes a difference
inTable = r'Layer\Fiber'
Apologies Rebecca, my code actually has the quote at the end. I forgot to type that in. But that doesn't seem to cause the error. Any other ideas?
Ah yes, typos happen. I saw the missing quote and didn't go beyond that since need to fix the obvious errors first. Looks like you have it resolved now.
Thank you for the help! It was greatly appreciated!
it is hard to provide an example without creating a class or running a file, but have you considered reversing your condition or do you want to completely bail?
if not(None in row[:-1]):
row[2] = row[0] * row[1]
cursor.updateRow(row)
I am open to anything Dan as long as it does what I need it to do. The path I am taking is to sort through the strandsField to first see if there are any NULL values, if there are any, return a message saying Insufficient Data. If there are no NULL values, then multiple the strandsField with the milesField. But I want the first conditional to run to inform users if there is insufficient data. Hopefully that makes sense!
Dan, I just tried it with the code you had listed but within the with statement:
with arcpy.UpdateCursor(fiberFeatures, fields) as cursor:
if not(None in row[:-1]):
row[2] = row[0] * row[1]
cursor.updateRow(row)
This still gives me the AttributeError: __Exit__
Instead of investing in fixing code that uses the old ArcPy cursors, I recommend spending a few minutes to read up on the not-so-new ArcPy Data Access cursors. They are much more efficient, and a bit more Pythonic, than the original Python cursors. UpdateCursor—Help | ArcGIS Desktop
That did the trick Joshua. Changing it from arcpy.UpdateCursor to arcpy.da.UpdateCursor.