'UnicodePython 3: EncodeError: 'ascii' codec can't encode character '\xe4'
I am trying to send some emails with pandas from an excel-file. I have this error for over a week now and even after hours of searching through SO, google, forums and so on, I just can't come u with an answer to fix the problem.
Here is the code:
import pandas as pd
import smtplib
your_name = "myname"
your_email = "mymail"
your_password = "mypw"
server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
server.ehlo()
server.login(your_email, your_password)
# Read the file
email_list = pd.read_excel("myfile.xlsx")
# Get all the Names, Email Addreses, Subjects and Messages
all_emails = email_list['Email']
all_messages = email_list['Text']
# Loop through the emails
for idx in range(len(all_emails)):
# Get each records name, email, subject and message
email = all_emails[idx]
message = all_messages[idx]
# Create the email to send
full_email = ("From: {0} <{1}>\n"
"To: <{2}>\n"
"Subject: My_Subject_Title\n\n"
"{3}"
.format(your_name, your_email, email, message))
# In the email field, you can add multiple other emails if you want
# all of them to receive the same text
try:
server.sendmail(your_email, [email], full_email)
print('Email to {} successfully sent!\n\n'.format(email))
except Exception as e:
print('Email to {} could not be sent :( because {}\n\n'.format(email, str(e)))
server.close()
I am getting the error:
'ascii' codec can't encode character '\xe4'
So obviously the error is caused by some european letters inside my excel file. What I tryied (along several others ways) was to encode the file:
email_list = pd.read_excel("myfile.xlsx", encoding=("utf-8"))
>>> TypeError: read_excel() got an unexpected keyword argument 'encoding'
or:
email_list = pd.read_excel("myfile.xlsx")
email_list.encode("utf-8")
>>> AttributeError: 'DataFrame' object has no attribute 'encode'
Non if it seems to work.
I'm happy if someone can help me out in what I`m doing wrong. Very new to python and these are my first real trys to implement some actual work-related problems.
Thanks a lot in advance!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
