AnsweredAssumed Answered

nesting UpdateCursor within SearchCursor and more...?

Question asked by wpalmer on Dec 17, 2012
Latest reply on Dec 18, 2012 by wpalmer
Hello All,

I am new to python and I'm in the learning/testing phase.

From borrowing various snipits, I am calculating the azimuth of a line segment then attempting to write that value to the attribute table. Here's my code, then I'll get to the question. 

import arcpy from arcpy import env from math import atan2, pi env.workspace = "c:/my_data/test_data.gdb" fc = "polyline1" # polyline1 has pre-created "azimuth" field and is a single segment feature class   #angleList []  angleMath = arcpy.da.SearchCursor(fc, ["OID@", "SHAPE@"])   for row in angleMath:     startpt = row[1].firstPoint     x1 = startpt.X     y1 = startpt.Y     endpt = row[1].lastPoint     x2 = endpt.X     y2 = endpt.Y     deltax = x2 - x1     deltay = y2 - y1     angle_rad = atan2(deltay,deltax)     angle_deg = angle_rad*180.0/pi          #angleList.append      cursor = arcpy.UpdateCursor(fc)     for upRow in cursor:         upRow.azimuth = angle_deg         cursor.updateRow(upRow)          del cursor  del angleMath

I am able to get the angle_deg value and my commented angleList list works but I'm not sure where/how to use it. I know my UpdateCursor is in the wrong spot. It updates the first angle_deg value for all records. Is there a way to use the UpdateCursor to write the value of the angle_deg variable for each record?

Or would it be best to try to use the angleList list with an UpdateCursor and if so, how do you use a list to feed an UpdateCursor?

Thank you for any help you can provide.