3

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 
xx@gmail.com  xxxx@vvv.com;yyy@gggfg.com;tiitioo@ggg.com
yy@xxx.com     ggg@vvv.com;yyy@gggfg.com;vvv@ggg.com
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 = "xx@xx.com"
    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()        
Hummer
  • 429
  • 1
  • 3
  • 16

4 Answers4

13

Let's try str.split and str.join:

import pandas as pd

df = pd.DataFrame({'SENDFROM': {0: 'xx@gmail.com', 1: 'yy@xxx.com'},
                   'Test': {0: 'xxxx@vvv.com;yyy@gggfg.com;tiitioo@ggg.com',
                            1: 'ggg@vvv.com;yyy@gggfg.com;vvv@ggg.com'}})

# Use str.split and str.join and astype
df['Test'] = df['Test'].str.split(';').str.join(',')

print(df.to_string())

Output:

       SENDFROM                                        Test
0  xx@gmail.com  xxxx@vvv.com,yyy@gggfg.com,tiitioo@ggg.com
1    yy@xxx.com       ggg@vvv.com,yyy@gggfg.com,vvv@ggg.com
Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
0

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.

ohtotasche
  • 478
  • 3
  • 7
0

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(['xxxx@vvv.com;yyy@gggfg.com;tiitioo@ggg.com'])

print(test)

>> 0    xxxx@vvv.com;yyy@gggfg.com;tiitioo@ggg.com
>> 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(';'))

>> ['xxxx@vvv.com', 'yyy@gggfg.com', 'tiitioo@ggg.com']

# 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       xxxx@vvv.com
>> 1      yyy@gggfg.com
>> 2    tiitioo@ggg.com
>> Name: index, dtype: object

Shout out to: Split (explode) pandas dataframe string entry to separate rows

raulindo
  • 187
  • 13
0

You can use pandas.Series.replace() to replace ; with ,

df['Test'] = df['Test'].replace(';', ',')
Ynjxsjmh
  • 28,441
  • 6
  • 34
  • 52