[GIS] Extracting data using arcpy.da.SearchCursor

arcgis-10.2arcpycursorlist

I have a shapefile of point features showing fire locations in a given year. One of the fields is named "YYYYMMDD" and indicates the corresponding date that that fire happened. In Python, I am trying to use the arcpy.da.SearchCursor function to append each date into a list and find the most recent fire by date.

My code looks like this:

listname = []
cursor = arcpy.da.SearchCursor(fires, "YYYYMMDD")

for row in cursor:
    listname.append(row)

print 'the first fire date is ',min(listname),'.'
print 'the last fire date is ',max(listname),'.'

What prints for min and max listname is: (20010515.0,) and (20011002.0,)
But what I would like to get as an output is: 20010514 and 20011002

How do can I get that as my output?

I tried indexing what I got to exclude what I don't need but that doesn't work.

Best Answer

You need to grab the first element of each row and convert to int.

listname = []
cursor = arcpy.da.SearchCursor(fires, "YYYYMMDD")

for row in cursor:
    listname.append(int(row[0]))

print 'the first fire date is {0}.'.format(min(listname))
print 'the last fire date is {0}.'.format(max(listname))
Related Question