Hello,
I'm trying to pull all of the unique dates out of an attribute field and I'm having issues formatting the output. Right now I have the following:
table = r"C:\Temp\SomeFile.shp"
field = "ADD_DATE"
valueList = []
rows = arcpy.SearchCursor(table)
for row in rows:
valueList.append(row.getValue(field))
uniqueSet = set(valueList)
uniqueList = list(uniqueSet)
uniqueList.sort()
del rows
del row
print uniqueList
The output is datetime.datetime(2017, 9, 19, 14, 28, 29) I'd like it to simply be 2017/09/19
Can anyone help?
formatteddatetime = yourdatevalue.strftime("%Y/%m/%d")
You will want to use the newer Data Access cursors: SearchCursor—Help | ArcGIS Desktop. Python's Counter class was specifically made for situations like this.
from collections import Counter
table = r"C:\Temp\SomeFile.shp"
field = "ADD_DATE"
cnt = Counter(dt for dt, in arcpy.da.SearchCursor(table,field))
for dt, freq in cnt.items():
if dt:
print dt.strftime("%Y/%m/%d")