Accessing attribute table datetime.datetime field

219
3
Jump to solution
02-16-2024 06:43 AM
NJDEPBrandonMcAlister
New Contributor III

Hey everyone, 

I have a dataset that has a field in the attribute table that contains datetime information. I am writing a script that selects three layers, copies them, delete extra fields and then exports the tables to an excel file. Everything I have written works correctly but the last bit is what I cannot figure out. The excel file name needs to end with the date the data was last updated. 

Currently in the attribute table there is a field called dataload_date which contains datetime information for when it was last updated. using this code:

field = "dataload_date"

testlist = []

with arcpy.da.SearchCursor(FeatureClass, field) as cursor:
         for row in cursor:

         testlist.append(str(row[0])[0:10])
         print(testlist[0])

This prints out the the date like this 2024-01-31, I need this as a string so I can manipulate it into the correct format for my file name.

I am under the impression that this code is only supposed to access the first row and append that value as a string to position 0 of test list. But it appears it iterates through every row in the dataset and appends it to position 0. 

NJDEPBrandonMcAlister_0-1708094599731.png

 

What am I doing wrong?

 

Code Sample came from: https://pro.arcgis.com/en/pro-app/latest/arcpy/data-access/searchcursor-class.htm

Thanks,
Brandon
0 Kudos
1 Solution

Accepted Solutions
DanPatterson
MVP Esteemed Contributor

cursors are designed to be cycled through, but if you want to just get some specific value, you can use one of the cursor methods to do this.  For example, I just want to see the first and last record in a searchcursor and print the values for those records.

import arcpy
fc00 = r"C:/arcpro_npg/Project_npg/npgeom.gdb/Ontario_bounds"
cursor = arcpy.da.SearchCursor(fc00, "*")
arr = cursor._as_narray()
frst = arr[0]
last = arr[-1]
print(frst)
print(last)
(1, [7559161.79374897, 1165505.11516148], 1, 20317.03064811, 9434393.51428649)
(205, [6373274.3835835 , 2297409.28244488], 1, 939.16177869, 28359.60787187)

I could get the field names etc, once I have the data, but that is another story.

# I forget the field names

arr.dtype.names
('OBJECTID', 'Shape', 'Diss_fld', 'Shape_Length', 'Shape_Area')

# what is the biggest polygon?

arr['Shape_Area'].max()
Out[20]: 1074121664275.803

# and so on

... sort of retired...

View solution in original post

0 Kudos
3 Replies
BlakeTerhune
MVP Regular Contributor

Use strftime() and the formatting codes to get the date/time formatted however you like.

datetime — Basic date and time types — Python 3.12.2 documentation

0 Kudos
DanPatterson
MVP Esteemed Contributor

cursors are designed to be cycled through, but if you want to just get some specific value, you can use one of the cursor methods to do this.  For example, I just want to see the first and last record in a searchcursor and print the values for those records.

import arcpy
fc00 = r"C:/arcpro_npg/Project_npg/npgeom.gdb/Ontario_bounds"
cursor = arcpy.da.SearchCursor(fc00, "*")
arr = cursor._as_narray()
frst = arr[0]
last = arr[-1]
print(frst)
print(last)
(1, [7559161.79374897, 1165505.11516148], 1, 20317.03064811, 9434393.51428649)
(205, [6373274.3835835 , 2297409.28244488], 1, 939.16177869, 28359.60787187)

I could get the field names etc, once I have the data, but that is another story.

# I forget the field names

arr.dtype.names
('OBJECTID', 'Shape', 'Diss_fld', 'Shape_Length', 'Shape_Area')

# what is the biggest polygon?

arr['Shape_Area'].max()
Out[20]: 1074121664275.803

# and so on

... sort of retired...
0 Kudos
NJDEPBrandonMcAlister
New Contributor III

Thanks Dan that worked perfectly 

Thanks,
Brandon
0 Kudos