Re: Populate fields using python (cumulative)

4499
44
10-14-2015 05:50 AM
TerryGustafson
Occasional Contributor II

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
JakeSkinner
Esri Esteemed Contributor

You can iterate through each RID and update the to_MEAS field.  Ex:

table = "Sample"

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
TerryGustafson
Occasional Contributor II

I get a 000539 : Error message from Python. It seems like it should work. At the end its taking the row[0] array and setting the toMeasList equal to it minus 1 record correct?

0 Kudos
TerryGustafson
Occasional Contributor II

Says syntax error line 1 which is declaring the table correct?

0 Kudos
JakeSkinner
Esri Esteemed Contributor

Yes, you will need to update the 'table' variable with the feature class/table in the Table of Contents.  Ex:

table = 'RoutingTable'

0 Kudos
TerryGustafson
Occasional Contributor II

I did that, the table is called result.  Does it have to be a certain type of table?

0 Kudos
JakeSkinner
Esri Esteemed Contributor

Can you zip and upload the table you are using?

0 Kudos
TerryGustafson
Occasional Contributor II

Ok, made a little headway. It appears to not like the null values in the fields.  When I: replace them with 0 then it does not error out at line 1 anymore.  :It then give me the warming.  "A Field name was not found or there were unbalanced quotation marks".  I was not sure if this was due to line 21 or 15..

0 Kudos
TerryGustafson
Occasional Contributor II

This is an image of the errors I receive in ArcMap

0 Kudos
DarrenWiens2
MVP Honored Contributor

I'm jumping into this thread midstream, but I'm confused why this has anything to do with Calculate Field. You're running this in the Python window, not the field calculator, correct?

In any case, it looks like however you are calling CalculateField, it is set to VB not Python.

0 Kudos