如何迭代数据框中的值并将值传递给 Python 中的发送电子邮件函数
How to iterate values from a dataframe and pass the values to a send email function in Python
我有一个数据框 (df)
如下所示
Subject To_Address CC_Address Backup_Address
Invoice 2345 is invalid xyz@gmail.com koke@gmail,ali@gmail.com a@gmail.com
Invoice 3456 is invalid abc@gmail.com jack@gmail.com,ali.gmail.com b@gmail.com
Invoice 3673 is invalid bcd@gmail.com peach@gmail.com,ali.gmail.com e@gmail.com
Invoice 8978 is invalid rock@gmail.com,ali.gmail.com c@gmail.com
Invoice 8078 is invalid rock@gmail.com,ali.gmail.com d@gmail.com
Invoice 3173 is invalid mnb@gmail.com mone@gmail.com,ali.gmail.com f@gmail.com
我需要使用
为数据框的每一行发送唯一的电子邮件
Email Subject = df[Subject] ,Email To Address =df[To_Address], Email_CC_Address = df[CC_Address]
还需要添加期望条件,Email To Address
为 null 或 NaN,则应传递 Email To Address =df[Backup_Address]
如何在 python 中完成此操作。
我当前使用静态值运行的代码
def create_message(send_from, send_to,cc_to,subject, plain_text_body):
message = MIMEMultipart('alternative')
message['From'] = send_from
message['To'] = COMMASPACE.join(send_to)
message['Cc'] = cc_to
message['Subject'] = subject
message.attach(MIMEText(plain_text_body, 'plain'))
return message
subject = "Invoice is invalid"
sender_address = "sender@gmail.com.com"
to = "to@gmail.com"
cc_to = "cc@gmail.com"
mail_content="This is a test message"
message = create_message(sender_address,[to],cc_to,subject,mail_content)
send_message(message)
要遍历 Pandas DataFrame,您可以使用 DataFrame.iterrows()
.
但是不推荐对 df 进行迭代,因为它被认为是“反模式”..
推荐阅读here
尝试使用 zip
:
在数据框中并行遍历行和多列
def create_message(send_from, send_to, cc_to,subject, plain_text_body):
sender_address = "sender@gmail.com.com"
mail_content="This is a test message"
for a, b, c, d in zip(df['Subject'], df['To_Address'], df['CC_Address'], df['Backup_Address']):
if b == '':
send_to = d
else:
send_to = b
subject = a
cc_to = c
message = MIMEMultipart('alternative')
message['From'] = send_from
message['To'] = COMMASPACE.join(send_to)
message['Cc'] = cc_to
message['Subject'] = subject
message.attach(MIMEText(plain_text_body, 'plain'))
return message
message = create_message(sender_address,[to],cc_to,subject,mail_content)
send_message(message)
我有一个数据框 (df)
如下所示
Subject To_Address CC_Address Backup_Address
Invoice 2345 is invalid xyz@gmail.com koke@gmail,ali@gmail.com a@gmail.com
Invoice 3456 is invalid abc@gmail.com jack@gmail.com,ali.gmail.com b@gmail.com
Invoice 3673 is invalid bcd@gmail.com peach@gmail.com,ali.gmail.com e@gmail.com
Invoice 8978 is invalid rock@gmail.com,ali.gmail.com c@gmail.com
Invoice 8078 is invalid rock@gmail.com,ali.gmail.com d@gmail.com
Invoice 3173 is invalid mnb@gmail.com mone@gmail.com,ali.gmail.com f@gmail.com
我需要使用
为数据框的每一行发送唯一的电子邮件Email Subject = df[Subject] ,Email To Address =df[To_Address], Email_CC_Address = df[CC_Address]
还需要添加期望条件,Email To Address
为 null 或 NaN,则应传递 Email To Address =df[Backup_Address]
如何在 python 中完成此操作。
我当前使用静态值运行的代码
def create_message(send_from, send_to,cc_to,subject, plain_text_body):
message = MIMEMultipart('alternative')
message['From'] = send_from
message['To'] = COMMASPACE.join(send_to)
message['Cc'] = cc_to
message['Subject'] = subject
message.attach(MIMEText(plain_text_body, 'plain'))
return message
subject = "Invoice is invalid"
sender_address = "sender@gmail.com.com"
to = "to@gmail.com"
cc_to = "cc@gmail.com"
mail_content="This is a test message"
message = create_message(sender_address,[to],cc_to,subject,mail_content)
send_message(message)
要遍历 Pandas DataFrame,您可以使用 DataFrame.iterrows()
.
但是不推荐对 df 进行迭代,因为它被认为是“反模式”..
推荐阅读here
尝试使用 zip
:
def create_message(send_from, send_to, cc_to,subject, plain_text_body):
sender_address = "sender@gmail.com.com"
mail_content="This is a test message"
for a, b, c, d in zip(df['Subject'], df['To_Address'], df['CC_Address'], df['Backup_Address']):
if b == '':
send_to = d
else:
send_to = b
subject = a
cc_to = c
message = MIMEMultipart('alternative')
message['From'] = send_from
message['To'] = COMMASPACE.join(send_to)
message['Cc'] = cc_to
message['Subject'] = subject
message.attach(MIMEText(plain_text_body, 'plain'))
return message
message = create_message(sender_address,[to],cc_to,subject,mail_content)
send_message(message)