Select to view content in your preferred language

Re: Populate fields using python (cumulative)

5921
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
TerryGustafson
Frequent Contributor

Hey Jake,

Is there any way to delete the last record in the table once the to_offset has been calculate?  I only want to generate the first segment 22.033 to 23.533 and remove the last record 0 to 23.533.

0 Kudos
TerryGustafson
Frequent Contributor

I thought this might work but no dice..

table = "Export_Output_3"
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"], "OBJECTID = '2'") as
cursor:
        for row in cursor:
    row[1] 
            cursor.deleteRow(row)
    del cursor
0 Kudos
DarrenWiens2
MVP Honored Contributor

Try:

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

Otherwise, provide error message.

0 Kudos
TerryGustafson
Frequent Contributor

I’m sure that will work but I keep getting a syntax error so must have a indent wrong.

0 Kudos
DarrenWiens2
MVP Honored Contributor

Well, line 26 (row[1]) looks wrong. Gee, an error message would sure be useful...

0 Kudos
TerryGustafson
Frequent Contributor

Sorry, I looked it up and I needed to use UpdateCursor but now I get the following error..

File "

TypeError: deleteRow() takes no arguments (1 given)

0 Kudos
DarrenWiens2
MVP Honored Contributor

deleteRow() deletes the current row, so no need to tell it which row.

cursor.deleteRow()

0 Kudos
TerryGustafson
Frequent Contributor

I tried that but it deletes both rows. I only wanted to delete the last row in the array after the to_measure was set.

0 Kudos
TerryGustafson
Frequent Contributor

I tried this but get a syntaxError at line 21 and don't see what the indent issue is..

table = "Export_Output_3"
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[1] == 2:
            cursor.deleteRow()
    del cursor
0 Kudos
DarrenWiens2
MVP Honored Contributor

You can (and possibly, must) remove all lines of 'del cursor'. Using the 'with' statement means the variable 'cursor' will be deleted on completion, so it may not be there to delete whatsoever.