Hello,
I have a shapefile that contains one line, and I need to extract the "Shape Length" value from his attribute table using python.
Any suggestion?
Solved! Go to Solution.
a search cursor will do the trick SearchCursor—ArcGIS Pro | Documentation
import arcpy
fc = 'c:/data/your_shapefile.shp'
#look at the geometry tokens in the help url
#for more understanding
fields = ['SHAPE@LENGTH']
#open cursor
with arcpy.da.SearchCursor(fc, fields) as cursor:
#for every row in the shapefiles attribute table
for row in cursor:
#row[0] is the first field specified when opening the cursor
#next field rould be row[1]...etc
#print the length
print(row[0])
Where do you want to extract it to?
Using Python Parser of Field Calculator?
!shape.length@<units>!
e.g.: For calculating length in yards
!shape.length@yards!
I only need the value of the "shape length" cell. Then, I will use this value in the script.
if there is a method to calculate the length of the polyline also it can work.
a search cursor will do the trick SearchCursor—ArcGIS Pro | Documentation
import arcpy
fc = 'c:/data/your_shapefile.shp'
#look at the geometry tokens in the help url
#for more understanding
fields = ['SHAPE@LENGTH']
#open cursor
with arcpy.da.SearchCursor(fc, fields) as cursor:
#for every row in the shapefiles attribute table
for row in cursor:
#row[0] is the first field specified when opening the cursor
#next field rould be row[1]...etc
#print the length
print(row[0])