通过电子邮件发送Python Dataframe可以通过以下步骤实现:
import pandas as pd
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
data = {'Name': ['John', 'Jane', 'Mike'],
'Age': [25, 30, 35],
'City': ['New York', 'London', 'Paris']}
df = pd.DataFrame(data)
html = df.to_html()
msg = MIMEMultipart()
msg['Subject'] = 'Python Dataframe'
msg['From'] = 'sender@example.com'
msg['To'] = 'recipient@example.com'
body = MIMEText(html, 'html')
msg.attach(body)
attachment = MIMEApplication(df.to_csv(index=False).encode('utf-8'))
attachment.add_header('Content-Disposition', 'attachment', filename='dataframe.csv')
msg.attach(attachment)
smtp_server = 'smtp.example.com'
smtp_port = 587
smtp_username = 'your_username'
smtp_password = 'your_password'
with smtplib.SMTP(smtp_server, smtp_port) as server:
server.starttls()
server.login(smtp_username, smtp_password)
server.send_message(msg)
以上代码将创建一个包含Dataframe内容的HTML邮件,并将Dataframe作为附件发送。确保替换smtp_server
、smtp_port
、smtp_username
和smtp_password
为您自己的SMTP服务器和凭据。
推荐的腾讯云相关产品:腾讯企业邮件(https://cloud.tencent.com/product/exmail)可以用于发送电子邮件,并提供了可靠的邮件传输服务。
领取专属 10元无门槛券
手把手带您无忧上云