Attributeerror: 'Series' Object Has No Attribute 'Split' Error in Sending Emails
How Can I Solve the Below Error. the Message Is as Below in Splitting the Test Emails with a Semi-Colon? Ideally I Should Send Emails from Sendfrom...
How can I solve the below error. The message is as below in splitting the Test emails with a semi-colon? Ideally I should send emails from Sendfrom corresponding emails in Test.
test
SENDFROM Test
;;
;;
AttributeError: 'Series' object has no attribute 'split'
My code is below:
import smtplib, ssl
from email.message import EmailMessage
import getpass
email_pass = getpass.getpass() #Office 365 password
# email_pass = input() #Office 365 password
context=ssl.create_default_context()
for idx, row in test.iterrows():
emails = test['Test']
sender_list = test["SENDFROM"]
smtp_ssl_host = 'smtp.office365.com'
smtp_ssl_port = 587
email_login = ""
email_from = sender_list
email_to = emails
msg2 = MIMEMultipart()
msg2['Subject'] = "xxx"
msg2['From'] = sender_list
msg2['To'] = ", ".join(email_to.split(";"))
msg2['X-Priority'] = '2'
text = ("xxxx")
msg2.attach(MIMEText(text))
s2 = smtplib.SMTP(smtp_ssl_host, smtp_ssl_port)
s2.starttls(context=context)
s2.login(email_login, email_pass)
s2.send_message(msg2)
s2.quit()
4 Answers
The email_to object is apparently a Series, not a string, so it does not have a split() method. The Series is already a sequence-like object, so you don't need to split it anyway. Do a type(email_to) to confirm this.
You can't use split to a Series Object.
From what I understood you want to do something like this:
import pandas as pd
test = pd.Series([';;'])
print(test)
>> 0 ;;
>> dtype: object
# You can see that the only and first row of Series s is a string of all
# emails you want to split by ';'. Here you can do:
# Apply split to string in first row of Series: returns a list
print(test[0].split(';'))
>> ['', '', '']
# I believe you can solve your problem with this list of emails.
# However you should code a loop to iterate for the remaing rows of initial Series.
# ----------------------------------------------------------------------------------
# Furthermore, you can explode your pandas Series.
# This will return you a DataFrame (ser), from which you can extract the info you want.
t = pd.concat([pd.Series(test[0], test[0].split(';'))
for _, row in test.iteritems()]).reset_index()
# Remove weird column
t.drop(labels=[0], axis=1, inplace=True)
# Convert DataFrame back to Series
t = t.squeeze()
# The info you probably want:
print(t)
>> 0
>> 1
>> 2
>> Name: index, dtype: object
Shout out to: Split (explode) pandas dataframe string entry to separate rows
You can use pandas.Series.replace() to replace ; with ,
df['Test'] = df['Test'].replace(';', ',')