【问题标题】:AttributeError: 'Series' object has no attribute 'split' error in sending emailsAttributeError:“系列”对象在发送电子邮件时没有属性“拆分”错误
【发布时间】:2021-07-27 08:11:52
【问题描述】:

如何解决以下错误。用分号分隔测试电子邮件时的消息如下?理想情况下,我应该从 Sendfrom 中的相应电子邮件发送电子邮件。

测试

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'

我的代码如下:

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()        

【问题讨论】:

    标签: python pandas smtp


    【解决方案1】:

    让我们试试str.splitstr.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())
    

    输出:

           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
    

    【讨论】:

      【解决方案2】:

      email_to 对象显然是 Series,而不是字符串,因此它没有 split() 方法。 Series 已经是一个类似序列的对象,所以无论如何都不需要拆分它。发送type(email_to) 以确认这一点。

      【讨论】:

      • 是的,我该如何解决?
      【解决方案3】:

      您不能将split 用于Series Object。 据我了解,您想做这样的事情:

      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
      
      

      大声疾呼:Split (explode) pandas dataframe string entry to separate rows

      【讨论】:

      • 刚刚我意识到您必须在您的系列中迭代并按每一行拆分电子邮件
      【解决方案4】:

      您可以使用pandas.Series.replace(); 替换为,

      df['Test'] = df['Test'].replace(';', ',')
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-01-12
        • 2019-10-16
        • 2020-11-09
        • 1970-01-01
        • 2015-07-14
        相关资源
        最近更新 更多