I have a table of 6900+ attributes within a feature class. I want to use the values of these attributes, specifically the fields 'DIST_Port' and 'Num_trips'. I want to run through these and provide a 'Transit Time' for all the attributes in the table.
As you can see, in the code below I have tried using the 'SearchCursor' to read the values of these fields, but I'm not sure how to use this to cycle through and complete the calculation for each attribute.
The output of this will be used in another tool, how can I create an output from the script, to be seen as an output in the model builder?

I would use CalculateField to complete this, but as this same formula is used multiple times throughout my model, but with different 'Lspeed' and 'ULspeed', I feel creating multiple fields would cram up the attribute table.

Here is my code so far:

import arcpy
import math

# Collect 'Loaded_Speed' and 'Unloaded_Speed'
Lspeed = arcpy.GetParameterAsText(0)
ULspeed = arcpy.GetParameterAsText(1)

# Set Workspace
arcpy.env.workspace = 'C:/Users/bnd75769/Documents/ArcGIS/Projects'/MyProject/MyProject.gdb

# Set Model Input Layer
modelInput = "LCOE_Output"

# Search Model Input for (Distance_to_Port and Number_Trips)

Dist_Port = arcpy.SearchCursor(modelInput, ["Dist_Port"])
Num_Trips = arcpy.SearchCursor(modelInput, ["Num_Trips"])

# Transit Time Formula
TransitTime = math.ceil((Dist_Port / Lspeed) + (Dist_Port / ULspeed) * Num_Trips / 24.0)

I'm relatively new to arcpy and python so any help would be great.

  • 1
    A cursor is not searching, it is returning each feature/row in a table one by one. You are using the old cursor, dont do that. Switch to da.SearchCursor instead. There is no need to use two cursors, look at the examples in the link. Start with something like with arcpy.da.SearchCursor(modelInput, ["Dist_Port","Num_Trips"]) as cursor: – BERA Apr 12 at 18:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.