Select to view content in your preferred language

Re: Populate fields using python (cumulative)

5922
44
10-14-2015 05:50 AM
TerryGustafson
Frequent Contributor

Ok I think I’m close but need a little more help.  I have a
table like the one attached.  I’m trying to create a line segment from it
that goes from 22.473 to 27.346 but they are on separate lines.  I need a
python script to calculate the to_measure from OBJECTID 1 equal to the
to_measure from OBJECTID2.  Do you think that is possible?

0 Kudos
44 Replies
DanPatterson_Retired
MVP Emeritus

The code is impossible to read since it is embedded so deep in the threads that it doesn't wrap properly...follow Darren's advice, stop mixing indendation levels and provide error messages.

0 Kudos
TerryGustafson
Frequent Contributor

I created sever types of table and set the table variable but seems to keep throwing the syntax at line 1.  Here is an example of an exported table and the code I used.

table = "Export_Output"
ridList = []
with arcpy.da.SearchCursor(table, ["RID"]) as cursor:
    for row in cursor:
        ridList.append(row[0])
del cursor
ridList = set(ridList)
for RID in ridList:
    toMeasList = []
    with arcpy.da.SearchCursor(table, ["RID", "to_MEAS"], "RID = '" + RID + "'") as cursor:
        for row in cursor:
            toMeasList.append(row[1])
    del cursor
    toMeasList.sort()
    with arcpy.da.UpdateCursor(table, ["to_MEAS"], "RID = '" + RID + "'") as cursor:
        for row in cursor:
            row[0] = toMeasList[-1]
            cursor.updateRow(row)
    del cursor
0 Kudos
Zeke
by
Honored Contributor

If you're using the 'with' syntax for your cursors, and you are, you don't need del cursor, as Darren pointed out. The cursor will automatically be deleted when the 'with' block is exited.

0 Kudos
TerryGustafson
Frequent Contributor

Thank you, I thought I posted that I was able to figure it out.. Here is what ended up working for me.

table = r"E:\MDTAPPS\terry\Default.gdb\points"
ridList = []
with arcpy.da.SearchCursor(table, ["RID"]) as cursor:
    for row in cursor:
        ridList.append(row[0])
del cursor
ridList = set(ridList)
for RID in ridList:
    toMeasList = []
    with arcpy.da.SearchCursor(table, ["RID", "to_MEAS"], "RID = '" + RID + "'") as cursor:
        for row in cursor:
            toMeasList.append(row[1])
    del cursor
    toMeasList.sort()
    with arcpy.da.UpdateCursor(table, ["to_MEAS"], "RID = '" + RID + "'") as cursor:
        for row in cursor:
            row[0] = toMeasList[-1]
            cursor.updateRow(row)
    del cursor
    toMeasList.sort()
    with arcpy.da.UpdateCursor(table, ["OBJECTID"]) as cursor:
 for row in cursor:
         if row[0] == 2:
             cursor.deleteRow()
    del cursor
0 Kudos
curtvprice
MVP Esteemed Contributor

No need to "del cursor" after the "with".  The "with" closes the cursor for you. See the examples in the help. But this is all moot as you should not be dinking with cursors inside a Calculate Field code block - the code block is already executed for each selected row in the input to Calculate Field!

As for your syntax error, I highlighted your code and found this immediately on line 10:

with arcpy.da.SearchCursor(table, ["RID", "to_MEAS"], "RID = '" + RID + "'""'")

your where expression has a syntax error. I like using the .format() method - easier to read and debug:

with arcpy.da.SearchCursor(table, ["RID", "to_MEAS"], "RID = '{}'".format(RID))

0 Kudos