gpt4 book ai didi

python - 将数据帧作为电子邮件正文 python 中的表格发送?

转载 作者:行者123 更新时间:2023-11-30 22:19:52 24 4
gpt4 key购买 nike

我有一个 csv 文件,需要将其作为电子邮件正文中的数据框发送。

但是,在我的电子邮件的输出中,我无法看到 csv 文件的列,而只是将 csv 文件的第一行作为列。

** 这是我的代码:**

import pandas as pd
import csv
from tabulate import tabulate
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import smtplib

me = 'myemailaddress@gmail.com'
password = 'password'
server = 'smtp.gmail.com:587'
you = 'myemailaddress@gmail.com'

text = """
Hello, Friend.

Here is your data:

{table}

Regards,

Me"""

html = """
<html>
<head>
<style>
table, th, td {{ border: 1px solid black; border-collapse: collapse; }}
th, td {{ padding: 5px; }}
</style>
</head>
<body><p>Hello, Friend This data is from a data frame.</p>
<p>Here is your data:</p>
{table}
<p>Regards,</p>
<p>Me</p>
</body></html>
"""

# with open('input.csv') as input_file:
# reader = csv.reader(input_file)
# data = list(reader)

data = pd.read_csv("MySampleFile.csv")
text = text.format(table=tabulate(data, headers="firstrow", tablefmt="grid"))
html = html.format(table=tabulate(data, headers="firstrow", tablefmt="html"))

message = MIMEMultipart(
"alternative", None, [MIMEText(text), MIMEText(html,'html')])

message['Subject'] = "First Attempt"
message['From'] = me
message['To'] = you
server = smtplib.SMTP(server)
server.ehlo()
server.starttls()
server.login(me, password)
server.sendmail(me, you, message.as_string())
server.quit()

最佳答案

看来我所要做的就是将 csv 文件中的相应列提取到一个单独的列表中,该列表可以传递到 tabulate 方法的 headers 参数中。

这是完整的解决方案

import pandas as pd
import csv
from tabulate import tabulate
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import smtplib

me = 'myemailaddress@gmail.com'
password = 'password'
server = 'smtp.gmail.com:587'
you = 'myemailaddress@gmail.com'

text = """
Hello, Friend.

Here is your data:

{table}

Regards,

Me"""

html = """
<html>
<head>
<style>
table, th, td {{ border: 1px solid black; border-collapse: collapse; }}
th, td {{ padding: 5px; }}
</style>
</head>
<body><p>Hello, Friend This data is from a data frame.</p>
<p>Here is your data:</p>
{table}
<p>Regards,</p>
<p>Me</p>
</body></html>
"""

# with open('input.csv') as input_file:
# reader = csv.reader(input_file)
# data = list(reader)

df = pd.read_csv("MySampleFile.csv")
col_list = list(df.columns.values)
data = df
# above line took every col inside csv as list
text = text.format(table=tabulate(data, headers=col_list, tablefmt="grid"))
html = html.format(table=tabulate(data, headers=col_list, tablefmt="html"))

message = MIMEMultipart(
"alternative", None, [MIMEText(text), MIMEText(html,'html')])

message['Subject'] = "First Attempt"
message['From'] = me
message['To'] = you
server = smtplib.SMTP(server)
server.ehlo()
server.starttls()
server.login(me, password)
server.sendmail(me, you, message.as_string())
server.quit()

关于python - 将数据帧作为电子邮件正文 python 中的表格发送?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48973840/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com