I am writing a script and a portion of said script requires that I build a table that starts at 0 and increments by 1 until reaching the value of "Shape_Length" from a feature class. I have line work that I wish to read the "Shape_Length" from and I am having troubles getting the syntax correct.
Route = arcpy.CreateRoutes_lr(Input_Linework, Linework_Field, Output_Name) # Store value in SHAPE_LENGTH from Route with arcpy.da.SearchCursor(Route, ['Shape_Length']) as cursor: for row in Cursor: Max_Value = row.getValue('Shape_Length') del row del cursor # Create table with two fields, RouteID and Chainage Route_Table = arcpy.CreateTable_management(str(Output_WS), Output_Name)) # Append additional rows to table in increments of 1 and end at the exact value of # the Max_Value fields = ['RouteID', 'Chainage'] Pop_Table = arcpy.da.InsertCursor(Route_Table, fields) for x in range(0, Max_Value): Pop_Table.insertRow(str(Linework_Field), x) del x
I get an error on line 6:
AttributeError: 'tuple' object has no attribute 'getValue'
I have tried reading through the page:
I messed around with the code a bit but this is where I am at now and can't determine how to move forward.
You are getting an AttributeError because you are instantiating Data Access cursors but using the old cursor syntax with them. Review the documentation for the Data Access cursors to see how to interact with them.
The 'row' in a search cursor is returned as a tuple and the elements there in are indexed just like a list.
fc = r'C:\path\to\featureClass fields = ['predir', 'name', 'postdir', 'posttype'] with arcpy.da.SearchCursor(fc,fields) as cursor: for row in cursor: print(row) ''' returns multi-element tuples: ('W', 'ARBOR PARK', None, 'DR') ('S', 'FLORABUND', None, 'LN') ('S', '8000', 'W', None) ('W', 'GETTYSBURG', None, 'DR') ('S', 'PATRICIA', None, 'CIR') ('W', 'HELEN', None, 'DR') ('W', 'LUCADIA', None, 'WAY') ('S', 'RITSON', None, 'LN') ('W', 'MAYTIME', None, 'DR') .... '''
In your case you only have row and as Joshua Bixby notes you'll want to adjust your code to something like this:
with arcpy.da.SearchCursor(fc,'SHAPE@LENGTH') as cursor: for row in cursor: print(row) ''' returns a single element tuples: (292.1710851770351,) (484.15425925311877,) (293.22981473912614,) (262.00460512592036,) (666.3035654334589,) (4628.563366540029,) (174.6003202336956,) .... '''
I'll let you you work on getting the max value...
To answer what Dan had said, I originally had it in that specific format. However I was faced with a different error:
AttributeError: 'float' object has no attribute 'getValue'
I understand that the Shape_Length field is a 'float' format data type but i am unsure as to why it wont let me store the value to be used later.
To respond to Joshua, I believe that your comment aligns with Dan's?
To answer Joe, I am aware that the 'row' in a search cursor is returned as a tuple and that it is indexed, hence the approach of what Joe had mentioned. For clarification in regard to your final statement "I'll let you you work on getting the max value...", the prerequisite for the tool I'm developing is a poly-line feature class with 1 row/feature of data, so the Max_Value is just the variable I store it as in order to append it to the table in line 17.
Thanks for the feedback guys I really appreciate it.
It sounds like Max_Value refers to the maximum number of records you'll write to your table, right? Somehow I construed it to mean the Max_Value of all the SHAPE@LENGTH token values. My mistake.
So what is the problem? You are getting value that happens to be a floating number, and you are applying this get.value method (that I'm not familiar with) to it and you are getting an error. Can you just set your Max_Value variable this way:
blah blah blah as cursor: for row in cursor: Max_Value = int(row)
Or am I still missing what it is you are after?
Max_Value is supposed to represent the value stored in the single only row of a polyline feature shape length. To better articulate my point:
Polyline feature class attribute table reads value "4263.344" in shape length field. I want to store that value in a variable so that in the next loop I can make a table that populates rows with 2 fields: "RouteID" and "Chainage"
RouteID will be used in order to make route events and match the routeID from the original polyline. The Chainage should start at value 0 then append a new row containing the next consecutive integer until it reaches the rounded down version of the stored "Max_Value" (Called_"Max_Round", my variables may not be descriptive enough). Then I want to append the final row with just the "Max_Value".
Output table should look like:
Basically am making meter by meter points along a route, adding coordinates, extracting the elevation from the DEM, resorting and renaming the fields (through "Add XY" tool, automatically calls the fields "POINT X" and "POINT Y" etc. so i just want them renamed for the final step) and final export into an excel worksheet. I have all the other parts working fine, because I stripped them from another tool i made last year. Always getting hung up on the cursors.
I'm stuck again trying to determine how to get the code in the table populating loop to work.
I got it now guys,
I am stuck on another part now but I am certain Ill be able to troubleshoot the rest. I was never great at the Cursors in school and it has now been a while since I have scripted. I am slowly relearning these things. Thank you all for your guidance.
The fixed code:
# Store value in SHAPE_LENGTH from Route with arcpy.da.SearchCursor(Route, ['Shape_Length']) as Cursor: for row in Cursor: Max_Value = row del row del Cursor
I'm stuck again trying to determine how to get the code in the table populating loop to work..(3:23)
I got it now guys... (1:06)
I've lost track of the order of your posts. Are you stuck or have you got your hands wrapped this beast?