【发布时间】:2020-01-06 13:13:06
【问题描述】:
我想用 gmail 发送邮件。但邮件必须明确包含一个数组。我需要帮助。 代码块中的消息部分,具体接受文本类型。
如果我具体说明我的目标;我有一个 .csv 文件,我正在从这个文件中创建随机子样本。子样本包括 5 行。我用这个随机行创建了一个数组,我想发送这个类型为数组的随机行。
import pandas as pd
import smtplib
data = pd.read_csv("Words1.csv")
row1 = data.sample(n=5)
A = [row1]
print(A)
email = 'sender@gmail.com' # Your email
password = 'senderpassword' # Your email account password
send_to_email = 'receiver@gmail.com' # Who you are sending the message to
message = 'A' # The message in the email
server = smtplib.SMTP('smtp.gmail.com', 587) # Connect to the server
server.starttls() # Use TLS
server.login(email, password) # Login to the email server
server.sendmail(email, send_to_email, message) # Send the email
server.quit() # Logout of the email server
谢谢。
【问题讨论】:
-
您需要“序列化”您的数组/列表。网上有很多关于如何在 python 中做到这一点的资源
-
pandas
.to_csv()没有输入的方法将返回一个 csv 字符串,所以只需执行A = row1.to_csv()你可以看到一个示例 here
标签: python arrays pandas email gmail