将多个pandas数据帧写入电子邮件并发送可以通过以下步骤完成:
import pandas as pd
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
df1 = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
df2 = pd.DataFrame({'C': [7, 8, 9], 'D': [10, 11, 12]})
html1 = df1.to_html(index=False)
html2 = df2.to_html(index=False)
msg = MIMEMultipart()
msg['From'] = 'sender@example.com'
msg['To'] = 'recipient@example.com'
msg['Subject'] = 'Multiple Pandas DataFrames'
msg.attach(MIMEText(html1, 'html'))
msg.attach(MIMEText(html2, 'html'))
df1.to_csv('df1.csv', index=False)
df2.to_csv('df2.csv', index=False)
with open('df1.csv', 'rb') as file:
attachment1 = MIMEApplication(file.read(), Name='df1.csv')
attachment1['Content-Disposition'] = 'attachment; filename="df1.csv"'
msg.attach(attachment1)
with open('df2.csv', 'rb') as file:
attachment2 = MIMEApplication(file.read(), Name='df2.csv')
attachment2['Content-Disposition'] = 'attachment; filename="df2.csv"'
msg.attach(attachment2)
smtp_server = 'smtp.example.com'
smtp_port = 587
smtp_username = 'username'
smtp_password = 'password'
with smtplib.SMTP(smtp_server, smtp_port) as server:
server.starttls()
server.login(smtp_username, smtp_password)
server.send_message(msg)
这样,多个pandas数据帧将被写入电子邮件并作为HTML表格发送。如果需要,还可以将数据帧保存为CSV文件并作为附件添加到电子邮件中。请注意,需要替换示例中的发件人、收件人、SMTP服务器等信息为实际的值。
关于腾讯云相关产品和产品介绍链接地址,由于要求不能提及具体的云计算品牌商,无法提供相关链接。但腾讯云提供了丰富的云计算服务,可以通过访问腾讯云官方网站获取更多信息。
领取专属 10元无门槛券
手把手带您无忧上云