我正在尝试使用 smtplib 通过 Python 发送邮件。我主要关心的是获取 csv/excel 的内容并将数据原样(表格格式)粘贴到正在发送的电子邮件的邮件正文中。我准备好以下代码片段来搜索文件并在 shell 上打印内容。如何在邮件正文中获得相同的输出?
from os import listdir
import csv
import os
#Search for a csv in the specified folder
directory = "folder_path"
def find_csv_filenames( path_to_dir, suffix="Data.csv" ):
filenames = listdir(path_to_dir)
return [ filename for filename in filenames if filename.endswith( suffix ) ]
filenames = find_csv_filenames(directory)
for name in filenames:
datafile=name
print(name)
path=directory+'//'+datafile
#Read the selected csv
with open(path,'r') as csvfile:
spamreader=csv.reader(csvfile,delimiter=' ',quotechar='|')
for row in spamreader:
print(', '.join(row))
TIA 为您提供帮助。