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
valid_from
androw[0]
are set to.if row[0].strip() == valid_from:
?