看板 Python 關於我們 聯絡資訊
各位先進好~ 最近小的再寫一隻簡單的撈資料.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
aweimeow: https://www.decalage.info/python/html 08/12 11:26
aweimeow: 你是從這邊找到的嗎,如果是的話,下方有下載連結哦! 08/12 11:27
CaptainH: 兩週內你從沒想過去學個html? 08/12 12:14
Chinfone: 我是用 prettytable ,也是跟你一樣的需求 08/12 13:51
a115073: CaptainH大,如果要用html的話本來就有實際在跑的php了.就 08/12 15:14
a115073: 是想學著利用python去解決所有問題,順便讓自己多學一點 08/12 15:15
a115073: aweimeow大,謝謝您,我再試試看!雖然我不是在這邊找到的.. 08/12 15:16
a115073: Chinfone大謝謝您,我也去試試看! 08/12 15:17