Reputation: 806
I have some problems with a loop. I have two lists, one per emails content and one for dates.
my_emails=['my email 1', 'my email 2', 'my email 3']
sent_date=['10/30/2020', '10/29/2020', '10/30/2020']
I am trying to run a for loop in order to do the following:
my email 1
was sent on 10/30/2020
; my email 2
was sent on 10/29/2020
, and my email 3
was sent on 10/30/2020
.
Per each couple email/date, I would need to look for emails sent 3 days before that date and 3 days after.
So, for example:my email 1
was sent on 10/30/2020
, then I would need to run some stuff for the next three days (i.e. for 10/31/2020, 11/01/2020, and 11/02/2020) ad for the three days before (i.e. 10/29/2020, 10/28/2020, and 10/27/2020).
I tried to do it as follows:
from datetime import datetime, timedelta
for i in my_emails:
print(i)
for t in sent_date:
t = datetime.strptime(t, '%m/%d/%Y')
start_date = t - timedelta(1) # it moves only one day before that date, but actually I should also consider the days after, as in the example
day = datetime.strftime(start_date, '%m/%d/%Y')
# do some stuff
The code does not return the expected range of dates. Can you please tell me how to include date information per each emails as in the list above, and how to run some code within the date range?
Thanks
My expected output would be:
since the date may change, I cannot consider a unique range for them, but considering the corresponding date in the list sent_date as starting point to calculate 3 days before and 3 days after that date.
Upvotes: 1
Views: 361
Reputation: 4130
Try this approach.
from datetime import datetime, timedelta
date_range = 3
for m,t in zip(my_emails, sent_date):
t = datetime.strptime(t, '%m/%d/%Y')
before_dates = [ t - timedelata(i) for i in range(1,date_range+1)]
after_dates = [ t + timedelata(i) for i in range(1,date_range+1)]
#do stuff with mail(m) and dates
Upvotes: 2