使用 Python 发送 Pandas DataFrame

Send Pandas DataFrame Using Python

在这里看到了一些类似的线程,但不知道如何使它起作用。

我想将 Pandas 数据帧封装到电子邮件中并发送出去。我希望 table 在电子邮件中显示为 table,而不是附件

电子邮件正文中的类似内容:

我尝试了下面的示例代码,但我一直收到错误消息:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-33-e766c6012525> in <module>()
     31 
     32 
---> 33 msg = MIMEText(content, text_subtype)
     34 msg['Subject']=       subject
     35 msg['From']   = sender # some SMTP servers will do this automatically, not all

C:\Program Files\Anaconda3\lib\email\mime\text.py in __init__(self, _text, _subtype, _charset)
     32         if _charset is None:
     33             try:
---> 34                 _text.encode('us-ascii')
     35                 _charset = 'us-ascii'
     36             except UnicodeEncodeError:

AttributeError: 'function' object has no attribute 'encode'

这是代码

import pandas as pd 

 Import_MICS="https://www.iso20022.org/sites/default/files/ISO10383_MIC/ISO10383_MIC.csv"
MICS=pd.read_csv(Import_MICS,sep=",")

SMTPserver = 'email-smtp.us-east-2.amazonaws.com'
sender =     'x.com'
destination = ['x.com']

USERNAME = "efsdff"
PASSWORD = "sfdfsf"

# typical values for text_subtype are plain, html, xml
text_subtype = 'html'


content="""\
Test message number 2
"""
content=MICS.to_html
subject="Sent from Python"

import sys
import os
import re

from smtplib import SMTP_SSL as SMTP       # this invokes the secure SMTP protocol (port 465, uses SSL)
from email.mime.text import MIMEText


msg = MIMEText(content, text_subtype)
msg['Subject']=       subject
msg['From']   = sender # some SMTP servers will do this automatically, not all



conn = SMTP(SMTPserver)
conn.set_debuglevel(False)
conn.starttls
conn.login(USERNAME, PASSWORD)
conn.sendmail(sender, destination, msg.as_string())

任何关于如何修复的想法将不胜感激

谢谢!

content=MICS.to_html

应该是

content=MICS.to_html()

我还必须为 csv 添加编码:

MICS=pd.read_csv(Import_MICS,encoding = 'ISO-8859-1')