2

I have to add a subject line to a list of other subject lines. This is normally an easy task, except this time, the email subject line always includes the date it was sent. How would I include this into my code?

Basically, I have the subject line "YYYY-MM-DD_Rest_of_Non-changing_Subject_Line" and I want to add anything that matches the first date part+the rest of the string to a list.

changning_date_subject = str(datetime.strptime('', '%Y-%m-%d')) + '_Rest_of_Non-changing_Subject_Line'
accepted_subjects = [changing_date_subject, 'Other Static Subject1', 'Other Static Subject2', 'Other Static Subject3']

I get ValueError: time data '' does not match format '%Y-%m-%d'", and while I understand why it would say that an empty string doesn't match, I can't really add a hard-coded string because it will change each time. Ideas? Thanks in advance!

1 Answers1

1

Taken from Getting today's date in YYYY-MM-DD in Python?

To get the current date in YYYY-MM-DD format, use:

from datetime import datetime

print(datetime.today().strftime('%Y-%m-%d'))

In your code, this would look like:

from datetime import datetime
changing_date_subject = datetime.today().strftime('%Y-%m-%d') + '_Rest_of_Non-changing_Subject_Line'
accepted_subjects = [changing_date_subject, 'Other Static Subject1', 'Other Static Subject2', 'Other Static Subject3']

Note: I fixed a typo in your variable from changning_date_subject to changing_date_subject

MurrayW
  • 393
  • 2
  • 10
  • 1
    And if it's not today, but any date? – trashdragon Aug 07 '19 at 13:23
  • How do you get the date value that you want to add to the subject line? e.g., does it come as a string, or as a datetime object? – MurrayW Aug 07 '19 at 13:28
  • It's as a string, basically the subject lines I get are: 2019-08-03_Rest_of_Non-changing_Subject_Line 2019-08-05_Rest_of_Non-changing_Subject_Line 2019-07-31_Rest_of_Non-changing_Subject_Line ^ For example. – trashdragon Aug 07 '19 at 13:32
  • Sorry for that hideous formatting! I get them as below, where the date isn't necessarily today: `2019-08-03_Rest_of_Non-changing_Subject_Line` `2019-08-05_Rest_of_Non-changing_Subject_Line` `2019-07-31_Rest_of_Non-changing_Subject_Line` – trashdragon Aug 07 '19 at 13:40
  • I may have got confused -- are you trying to extract the date out of the subject line? – MurrayW Aug 07 '19 at 13:42
  • Nope! I just want to add the string that's in the subject line to a list, but the problem is that the date changes, thereby changing the string. I just want it to add any date that it may start with to the list, regardless of what date it is - it's not important. Is that clearer? Thanks for the help :) – trashdragon Aug 07 '19 at 13:45
  • Where I'm running aground is that if you are already getting the subject (from some other process etc) - with the date already at the front - then to me it seems like you would just add the subject to your list, no need to manipulate anything. Maybe a better question from me is: why does it matter if the date part of the subject changes? What do you intend to do with it where that makes a difference? – MurrayW Aug 07 '19 at 13:52
  • I was just trying to retroactively get attachments from those emails, without trying to hard-code it for those dates. But going forward, I can use the "today" function so that's helpful anyways! – trashdragon Aug 07 '19 at 14:00