[問題] 將mysql query結果利用smtplib寫入並寄出
各位先進好~
最近小的再寫一隻簡單的撈資料.py
最後有一個小小的問題卡住了。
我希望由mysql撈出來的資料利用html寫成表格並寄出。
網路上僅查到N年前有個 HTML.table(?),但在python 2.7上並沒有這個函式庫
想請教各位有什麼其他的方法可以達到這個目標。
下方是小弟寫的:
#!/usr/bin/env python
import MySQLdb
import datetime
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
time = datetime.datetime.now().strftime('%Y-%m-%d')
now_time = time+"%"
#connect db & query
db = MySQLdb.connect('host', 'user', 'passwd','db_name')
cursor = db.cursor()
cursor.execute(""" mysql command""")
data = cursor.fetchall()
list = (enumerate(data))
#test on pycharm
for r,row in enumerate(list) :
for c,col in enumerate(row):
print col
#login & send mail using smtplib
sender='someone@mail'
recipient = 'someone@mail'
msg = MIMEMultipart('alternative')
msg['Subject'] = "Test"
msg['From'] = sender
msg['To'] = recipient
text = "blah blah"
html = str(data)
part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'table')
msg.attach(part1)
msg.attach(part2)
s = smtplib.SMTP()
s.connect(host='mail_host')
s.ehlo()
s.starttls()
s.login("user","passwd")
s.sendmail(sender, recipient, msg.as_string())
#...下略
在html = str(data)這邊確實會顯示出查詢結果,但就是一大串的查詢結果...
e.g (('a','b','c'),('d','e','f'),('g','h','i'))
每一個()都是一次的查詢。
希望有大大可以給小弟一點方向..這個問題困擾我兩週了 = =
文長傷眼抱歉~
謝謝!各位
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 203.67.9.60
※ 文章網址: https://www.ptt.cc/bbs/Python/M.1470965337.A.787.html
推
08/12 11:26, , 1F
08/12 11:26, 1F
→
08/12 11:27, , 2F
08/12 11:27, 2F
推
08/12 12:14, , 3F
08/12 12:14, 3F
→
08/12 13:51, , 4F
08/12 13:51, 4F
→
08/12 15:14, , 5F
08/12 15:14, 5F
→
08/12 15:15, , 6F
08/12 15:15, 6F
→
08/12 15:16, , 7F
08/12 15:16, 7F
→
08/12 15:17, , 8F
08/12 15:17, 8F
討論串 (同標題文章)
完整討論串 (本文為第 1 之 2 篇):
Python 近期熱門文章
PTT數位生活區 即時熱門文章