Python无法将csv附加到电子邮件中

时间:2016-10-11 15:08:20

标签: python csv smtplib

我是编程的初学者。使用Python(3.5),我试图创建一个从Web上擦除表格的脚本,并将通过电子邮件发送的csv写入收件人。感谢stvrflw的各种问题,我已经成功地完成了所有工作。 但问题是,我的csv附件是空的?!?我错过了什么? 谢谢!

....
#code above is to scrape info and create csv specified below. Works just fine!

        header="Date;Index;Country;%;+/-;Latest;High;Low;%1mon;%3mon;%6mon;%year;%1yr;Time"
    file=open(os.path.expanduser("Windex"+dte+".csv"),"wb")
    file.write(bytes(header, encoding="ascii",errors='ignore'))
    file.write(bytes(kdata, encoding="ascii",errors='ignore'))

    SUBJECT="Windex"+dte

    msg = MIMEMultipart()
    msg['From'] = 'xxx@xxx.com'
    msg['To'] = 'xxx@xxx.com'
    msg['Date'] = formatdate(localtime=True)
    msg['Subject'] = SUBJECT

    files= ['Windex'+dte+'.csv']

    for f in files:
        part = MIMEBase('application', "octet-stream")
        part.set_payload(open(f,"rb").read())
        encoders.encode_base64(part)
        part.add_header('Content-Disposition', 'attachment; filename="{0}"'.format(os.path.basename(f)))
        msg.attach(part)

    server = smtplib.SMTP('smtp.gmail.com',587)
    server.ehlo()
    server.starttls()
    server.login('xxx','xxx')
    server.sendmail('xxx', 'xxx', msg.as_string())

0 个答案:

没有答案