Select to view content in your preferred language

UpdateCurosr if date field has date

630
5
12-13-2022 12:39 PM
TonyAlmeida
Occasional Contributor II

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)

 

 

 

 

0 Kudos
5 Replies
JoshuaSharp-Heward
Occasional Contributor III

Couple things to clarify from the above code.

  1. The first update cursor (rows 2-4) doesn't seem necessary as it is just a repeat of the lines below?
  2. A single "=" is used to assign a value, you need to use "==" to test for equality (e.g. row 7)
  3. On row 7 it appears you're trying to check the "Verified" field but referring to row[0], when it should be row[1]
  4. In row 8 you're trying to test to see whether the date field has a value in it, but you've already done this in line 6
  5. There seems to be a lot of repetition post row 17
  6. Are you just wanting to print the dates and not do anything with them? If so you could just do
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)

 

TonyAlmeida
Occasional Contributor II

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".

 

0 Kudos
JoshuaSharp-Heward
Occasional Contributor III

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?

0 Kudos
TonyAlmeida
Occasional Contributor II

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"

 

 

0 Kudos
JoshuaSharp-Heward
Occasional Contributor III

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!

0 Kudos