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.
What am I doing wrong?
Code Sample came from: https://pro.arcgis.com/en/pro-app/latest/arcpy/data-access/searchcursor-class.htm
Solved! Go to Solution.
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
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
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
Thanks Dan that worked perfectly