【发布时间】:2020-07-05 10:19:29
【问题描述】:
有发送电子邮件的代码,有些行与发送电子邮件的人的姓名相同,但每一行都有一个唯一的值。我试图让它发送一封电子邮件,其中包含名称列中具有相同名称的重复单元格的所有值,但这些行中的所有值都是唯一的。
因此,为此我尝试通过提取具有重复行的行来制作重复的数据框,以使 if else 语句更容易。因为当我发送电子邮件时,它会多次在不同的电子邮件中发送相同的值,所以我尝试制作它,所以每次它发送一封电子邮件时,它都会删除它使用的值的行。
在代码的末尾,它不是从单个值中删除单行,而是删除具有值的每一行。
import smtplib
import pandas as pd
import ssl
import xlrd
your_name = "Name Here"
your_email = "Email you using here"
your_password = "password to email here"
cc = input("Email of person you want to cc in email: ")
# for gmail change smtp-mail.outlook.com to smtp.gmail.com, 465
server = smtplib.SMTP_SSL('smtp-mail.outlook.com', 587)
server.ehlo()
server.login(your_email, your_password)
data = [['bob', 'testemail@gmail.com', 'howdy'], ['joe',
'testemail@gmail.com', 'hi'], ['bill', 'testemail@gmail.com', 'hey'],
['bob', 'testemail@gmail.com', 'hola'],['bob', 'testemail@gmail.com',
'hello'], ['josh', 'testemail@gmail.com', 'yo'], ['austin',
'testemail@gmail.com', 'cya']
df = pd.DataFrame(data, columns = ['Pending Action From', 'Email',
'values'])
all_names = email_list['Pending Action From']
all_emails = email_list['Email']
all_values = email_list['values']
# Takes duplicate row based off same name
duplicateRowsDF = email_list[email_list.duplicated(['Pending Action From'])]
duplicateRowsDF.to_excel('DuplicateQARList.xlsx', index=False)
duplicateRowsDF = pd.read_excel('DuplicateQARList.xlsx')
# removes duplicate row based off same name and keeps first same name
email_list.drop_duplicates(subset='Pending Action From', keep="first", inplace=True)
# email_list.to_excel('TestQARList.xlsx')
duplicate_values = duplicateRowsDF['value']
duplicate_names = duplicateRowsDF['Pending Action From']
# Create the email to send
def full_email():
full_email = ("From: {0} <{1}>\n"
"To: {2} <{3}>\n"
"Cc: {4}\n"
"Subject: {5}\n\n"
"{6}"
.format(your_name, your_email, name, email, cc,
subject, message))
# Create the email to send
# 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)))
for idx in range(len(email_list)):
email = all_emails[idx]
name = all_names[idx]
value = all_values[idx]
for i in range(len(duplicateRowsDF)):
duplicate_value = duplicate_values[i]
duplicate_name = duplicate_names[i]
if name == duplicate_name and value != duplicate_value and duplicate_names[i] == duplicate_names[i]:
message = value, duplicate_values[i]
full_email()
email_list = email_list.drop(value)
# Close the smtp server
server.close()
【问题讨论】:
-
欢迎堆栈溢出!您能否提供一个minimal reproducible example,包括您的输入数据框样本,以便我们更好地了解您的问题?似乎您可以使用 groupby,但很难说
-
感谢您的反馈,我添加了一些图片让我知道是否有帮助。谢谢!
-
感谢更新,但在问题的文本中包含示例数据很重要,而不是图片或链接。我们无法重现或测试数据图片。见How to make good pandas examples
-
知道了,应该包括所有内容。我刚刚在文件中复制了数据框。谢谢!
-
我仍然不清楚你的任务,但是使用
df.groupby(['Pending Action From','Email']).agg(list)将所有'values'按名称/电子邮件分组,然后采取行动是否有用?那你就不用担心重复的名字了,你可以pop每一行并处理它来发送电子邮件...?
标签: python pandas dataframe email xlrd