我想将pandas数据帧数据作为HTML电子邮件发送。根据{{3}}帖子,我可以使用数据框创建一个html。代码
import pandas as pd
import numpy as np
HEADER = '''
<html>
<head>
</head>
<body>
'''
FOOTER = '''
</body>
</html>
'''
df = pd.DataFrame([[1.1, 1.1, 1.1, 2.6, 2.5, 3.4,2.6,2.6,3.4,3.4,2.6,1.1,1.1,3.3], list('AAABBBBABCBDDD')]).T
with open('test.html', 'w') as f:
f.write(HEADER)
f.write(df.to_html(classes='df'))
f.write(FOOTER)
现在我想将其作为html电子邮件发送。我试过了this。无法弄清楚如何附加html文件?
答案 0 :(得分:6)
Pandas具有以下功能:http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_html.html
这将为表格提供html代码,之后您可以将其嵌入到以下电子邮件中:
df = DataFrame(data)
email = " some html {df} lah lah"
email = email.format(df=df.to_html())
答案 1 :(得分:3)
filename = "test.html"
f = file(filename)
attachment = MIMEText(f.read(),'html')
msg.attach(attachment)