【问题标题】:Send Pandas DataFrame Using Python使用 Python 发送 Pandas DataFrame
【发布时间】:2018-04-08 01:14:25
【问题描述】:

在这里看到了一些类似的主题,但不知道如何使这项工作发挥作用。

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

电子邮件正文中的内容如下:

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

---------------------------------------------------------------------------
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())

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

谢谢!

【问题讨论】:

    标签: python pandas email dataframe mime


    【解决方案1】:
    content=MICS.to_html
    

    应该是

    content=MICS.to_html()
    

    我还必须为 csv 添加编码:

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

    【讨论】:

      猜你喜欢
      • 2021-10-15
      • 2022-01-11
      • 2020-10-10
      • 2018-04-09
      • 1970-01-01
      • 2013-12-16
      • 2021-01-01
      • 1970-01-01
      • 2020-06-14
      相关资源
      最近更新 更多