N0rthstarCSD

Code for leaving a value alone if a condition is not met

Discussion created by N0rthstarCSD on Mar 10, 2014
Latest reply on Mar 11, 2014 by N0rthstarCSD
Hello -
I have a script that updates a field based on a date in another field. 

If the date in the field "Compliance_Date2013" is over a year old, the script changes the value to "expired"

Otherwise, in the way that this script is written, the value is changed to "NotRecorded"

Since i have a value that indicates a value for the previous year, I'd like the script to simply leave the column alone if it doesn't meet the date criteria, instead of inserting the "NotRecorded" value.

Thank you so much for any help!

Here is the script:

import arcpy, os, datetime, re 

from datetime import timedelta

# Geodatabase connection file/Feature Dataset/Feature Class
table = r"C:\Users\Administrator\AppData\Roaming\ESRI\Desktop10.1\ArcCatalog\Connection to localhost.sde\Defensible_Space\Inspections_2014"
desc = arcpy.Describe(table)
fields = desc.fields
#fields defined
inspectionField = "Compliance_2014"
inspectDate = "Compliance_Date2013"
# todayDate set for today
todayDate = datetime.date.today()


# Open a Updatecursor 
rows = arcpy.UpdateCursor(table) 
# Iterate through the rows in the cursor 
# 
for row in rows:
    #get the values from the inspection date and pass fail fields
    inspectDateStr = str(row.getValue(inspectDate))
    status = row.getValue(inspectionField)
    if inspectDateStr == "None":
       row.setValue(inspectionField, "NotRecorded")
       rows.updateRow(row)
    else:
        #If the inspection date field value is != None then split the field value based on -
        year, month, day = inspectDateStr.split('-')
        #remove the time from the day
        abrvday = day[:-9]
        # convert the row's value to year/month/day in datetime
        featureDate = datetime.date(int(year), int(month), int(abrvday))
        #find the difference in 365 days
        timediff = todayDate - featureDate
        daysinayear = datetime.timedelta(days=365)
        if timediff>=daysinayear:
            row.setValue(inspectionField,"Expired")
            rows.updateRow(row)
            
        else:
             pass
#removes rows from inmemory
del rows

Outcomes