I am trying to figure our how to update a field based on two other fields, first field is a text field and the other is a date field. I am trying to figure out how to check if the Datefield field is populated with a date. So if the first and second filed have those parameters I need the third field updated like below. The date field either has a date or it's just empty/blank. I would also like to print the dates but I can't seem to get the format/ syntax right. Date field is month, day, year.
with arcpy.da.UpdateCursor(lyr,['datefield', 'Verified', 'update']) as cursor:
for row in cursor:
if row[0] not in (""," ",None):
if row[1] = "Verified" # text
if row[0]= "date" #check if any date is in field, field has dates 09/10/2021.
#print the dates
#date = datetime.datetime.strftime((row[0]),"%m/%d/%y")
#print (date)
row[2] = "Yes Verified, with date"
else:
row[2] = "Not Verified, no date"
cursor.updateRow(row)
Couple things to clarify from the above code.
print(row[0].strftime("%m/%d/%Y"))
Based on my interpretation of the code you'd and your description something like this should work, assuming that the only two options for the update field are "Yes Verified, with date" and "Not Verified, no date".
from datetime import datetime
with arcpy.da.UpdateCursor(lyr,['datefield', 'Verified', 'update']) as cursor:
for row in cursor:
if row[0] not in (""," ",None):
print(row[0].strftime("%m/%d/%Y")
if row[1] == "Verified":
row[2] = "Yes Verified, with date"
else:
row[2] = "Not Verified, no date"
cursor.updaterow(row)
Sorry about that, not sure what happened when I pasted my code. I have fixed it now.
On my fixed code, I have to check to make sure that datefield has a date, if there is a date and the field Verified has "Verified", then update the update field with "Yes verified, with date". if those two paramerters are not met then update the update field with "Not verified, no date".
I think the code I posted should do exactly that then. The third row of your code (4th of mine) should determine whether or not there is a value in the date field, and then if it's got a value of "Verified" then write "Yes Verified, with date" to the update field, and if there's no value in the date field then it will default to "Not Verified, no date". The only thing that is missing is handling cases where there is a date but no "Verified" value, is that possible in your data?
I was able to get it with the following.
with arcpy.da.UpdateCursor(lyr,['datefield', 'Verified', 'update']) as cursor:
for row in cursor:
if row[0] not in (""," ",None):
if row[1] = "Verified" # text
if row[0].date
print (row[0].strftime("%m/%d/%Y"))
row[2] = "Yes Verified, with date"
else:
row[2] = "Not Verified, no date"
I'm not sure how row 4 or 5 are running correctly in that code, but if it works for you I'm glad it's sorted!