I am making a program to send people happy birthday emails on their birthday. i keep getting the error in the title. From every question i have read on here it appears i can't get hold of an integer in my dictionary, but that doesn't make any sense.
LETTERS = ["letter_1.txt", "letter_2.txt", "letter_3.txt"]
bd = p.read_csv("birthdays.csv")
data = bd.to_dict(orient="index")
now = d.datetime.now()
day = now.day
month = now.month
for d in data:
bday = d['day']
bmonth = d['month']
if day == bday and month == bmonth:
recipient = d['email']
letter_choice = r.choice(LETTERS)
with open(f"./letter_templates/{letter_choice}", mode="r") as l:
orig_letter = l.read()
r_letter = orig_letter.replace(PLACEHOLDER, d['name'])
with s.SMTP("smtp.gmail.com") as connection:
connection.starttls()
connection.login(user=MY_EMAIL, password=PASSWORD)
connection.sendmail(
from_addr=MY_EMAIL,
to_addrs=recipient,
msg=f"subject:Happy Birthday\n\n{r_letter}"
)
how can i get my if statement to check the day and month from my dictionary?
Traceback (most recent call last):
File "C:\Users\hoods\zip_downloads_for_class\birthday-wisher-hard-start\main.py", line 19, in <module>
bday = d['day']
~^^^^^^^
TypeError: 'int' object is not subscriptable
CodePudding user response:
It looks like data
is a dictionary with integer keys. You most likely want to iterate through its values instead of keys.
LETTERS = ["letter_1.txt", "letter_2.txt", "letter_3.txt"]
bd = p.read_csv("birthdays.csv")
data = bd.to_dict(orient="index")
now = d.datetime.now()
day = now.day
month = now.month
for d in data.values(): # <---
bday = d['day']
bmonth = d['month']
if day == bday and month == bmonth:
recipient = d['email']
letter_choice = r.choice(LETTERS)
with open(f"./letter_templates/{letter_choice}", mode="r") as l:
orig_letter = l.read()
r_letter = orig_letter.replace(PLACEHOLDER, d['name'])
with s.SMTP("smtp.gmail.com") as connection:
connection.starttls()
connection.login(user=MY_EMAIL, password=PASSWORD)
connection.sendmail(
from_addr=MY_EMAIL,
to_addrs=recipient,
msg=f"subject:Happy Birthday\n\n{r_letter}"
)
CodePudding user response:
It appears that the data dictionary consists of individual data points as dictionaries themselves. But in the code, d is used as the looping variable to represent each of these dictionaries, not the individual values.
Try changing d to person or another more descriptive name to avoid confusion, and use this new name to access the individual values in the dictionary, like so:
for person in data:
bday = data[person]['day']
bmonth = data[person]['month']
if day == bday and month == bmonth:
recipient = data[person]['email']
letter_choice = r.choice(LETTERS)
with open(f"./letter_templates/{letter_choice}", mode="r") as l:
orig_letter = l.read()
r_letter = orig_letter.replace(PLACEHOLDER, data[person]['name'])
with s.SMTP("smtp.gmail.com") as connection:
connection.starttls()
connection.login(user=MY_EMAIL, password=PASSWORD)
connection.sendmail(
from_addr=MY_EMAIL,
to_addrs=recipient,
msg=f"subject:Happy Birthday\n\n{r_letter}"
)