AttributeError: 'Series' object has no attribute 'split' error in sending emails
AttributeError: 'Series' object has no attribute 'split' error in sending emails
如何解决以下错误。消息如下,用分号分隔测试邮件?理想情况下,我应该从 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()
email_to
对象显然是一个 Series
,而不是字符串,因此它没有 split()
方法。 Series
已经是一个类似序列的对象,因此您无论如何都不需要拆分它。做一个 type(email_to)
来确认这一点。
您不能将 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
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
您可以使用 pandas.Series.replace() 将 ;
替换为 ,
df['Test'] = df['Test'].replace(';', ',')
如何解决以下错误。消息如下,用分号分隔测试邮件?理想情况下,我应该从 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()
email_to
对象显然是一个 Series
,而不是字符串,因此它没有 split()
方法。 Series
已经是一个类似序列的对象,因此您无论如何都不需要拆分它。做一个 type(email_to)
来确认这一点。
您不能将 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
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
您可以使用 pandas.Series.replace() 将 ;
替换为 ,
df['Test'] = df['Test'].replace(';', ',')