2

I have a large script, in which a part of it wants to check whether a shapefile has attributes in the "Valid_From" date field. If they match the date variables already created, these need to be deleted. Im testing this using a shapefile that already meets the criteria, however the rows are not being deleted.

from dateutil.relativedelta import relativedelta    
today = datetime.date.today()    
date_after_month = datetime.datetime.today() + relativedelta(months=1)    
valid_from = date_after_month.strftime('01/%m/%Y')
valid_to = str(calendar.monthrange(date_after_month.year, date_after_month.month)[1]) + date_after_month.strftime("/%m/%Y")

Now, in the earlier parts of a script, I update a different shapefile using the date variables set above successfully with the following code:

with arcpy.da.UpdateCursor(rtc_shp, ["valid_from", "valid_to", "type"]) as cursor:
    for row in cursor:
        row[0] = valid_from
        row[1] = valid_to
        row[2] = "Offshore"
        cursor.updateRow(row)

However I now have to check whether a seperate shapefile already has this data, and if so, delete the rows. I use the following code, but it does not give the same results. It wont delete the rows. Which is strange, as the same variables are used to update the fields in the above example.

with arcpy.da.UpdateCursor(corporate_rtc_dataset, ["valid_from"]) as cursor:
    for row in cursor:
        if row[0] == valid_from:
            cursor.deleteRow()

print row 
2016-10-01 00:00:00

print valid_from
01/10/2016

UPDATE:

I have now tried to change the valid_from variable to match the row, however again, its not deleting the row:

valid_from = date_after_month.strftime('%Y-%m-01 00:00:00')

print valid_from
2016-10-01 00:00:00
9
  • 1
    Try adding print statements to see what valid_from and row[0] are set to.
    – PolyGeo
    Commented Sep 29, 2016 at 10:41
  • added those print statements. They differ. However I used the same variables to update the dates in a earlier version of a different shapefile, so not sure why its different now, and doesnt work.
    – MacroZED
    Commented Sep 29, 2016 at 10:52
  • Make sure they are the same type
    – Bera
    Commented Sep 29, 2016 at 11:11
  • What do you mean @Björn? The variables and the field types are both dates.
    – MacroZED
    Commented Sep 29, 2016 at 11:12
  • Does it work if you change the check to if row[0].strip() == valid_from:? Commented Sep 29, 2016 at 11:15

1 Answer 1

5

You can't compare a string with a datetime field (it will always be false).

Instead create valid_from like this:

date_after_month = datetime.datetime.today() + relativedelta(months=1)    
valid_from = date_after_month.replace(day=1,hour=0,minute=0,second=0,microsecond=0)

This will create a datetime object with the day set to 1 (beginning of the month) and time set to 00:00:00.

1
  • 2
    @MacroZED - And evil, don't forget evil.
    – Joseph
    Commented Sep 29, 2016 at 11:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.