[英]Combile multiple CSV Contents into email body with python
I have Multiple CSV files like below CSV1 and CSV2 and need to combine both csv into email body using python 我有多个CSV1和CSV2以下的CSV文件,需要使用python将两个csv合并到电子邮件正文中
CSV1 :-
S.No Name Student
1 Rishabh Engg
2 Samee Engg
CSV1 :-
S.No Name Student
1 Ankit Med
2 Arjun Engg
=====Expected output i need is like below :-===========
Dear Rishabh ,
PFB the Data :-
CSV1 :-
S.No Name Student
1 Rishabh Engg
2 Samee Engg
CSV1 :-
S.No Name Student
1 Ankit Med
2 Arjun Engg
Regards, Rishabh Jaiswal 此致Rishabh Jaiswal
========================= ========================
FYI , I have tried and sucessfully help to do single csv into email body with below code :- 仅供参考,我已经尝试并成功地通过以下代码帮助将单个csv制成电子邮件正文:-
with open('input.csv') as input_file:
reader = csv.reader(input_file)
data = list(reader)
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'] = "Your data"
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()
In order to combine the data from both csv files, you need to be reading from both of them. 为了合并两个csv文件中的数据,您需要从两个文件中读取数据。 A simple way to do this:
一种简单的方法:
import csv
with open('csv1.csv', 'r') as csv1, open('csv2.csv') as csv2:
reader1 = csv.reader(csv1, delimiter=',')
reader2 = csv.reader(csv2, delimiter=',')
all_text = str()
for row in reader1:
all_text += ' '.join(row) + '\n'
for row in reader2:
all_text += ' '.join(row) + '\n'
print all_text
This will successfully combine the data from the csv files into a singular string. 这将成功地将csv文件中的数据合并为单个字符串。 You can play around with the formatting of the all_text string.
您可以尝试使用all_text字符串的格式。 As for sending the email, it seems as though you can successfully do SMTP through python.
至于发送电子邮件,似乎您可以通过python成功完成SMTP。
There is also a neat python package called mailmerge that templates emails from csv data https://github.com/awdeorio/mailmerge , I'm not sure if you are templating emails from csv file data, or what your motives are, but maybe this will be useful for you :) 还有一个名为mailmerge的整洁的python程序包,可以将来自csv数据的电子邮件模板化为https://github.com/awdeorio/mailmerge ,我不确定您是否要从csv文件数据中提取电子邮件,或者您的动机是什么,但是也许这将对您有用:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.