2010-03-20 33 views

回答

1

藉助第三方項目mysqldb安裝,你可以輕鬆地閱讀該表,例如:

import MySQLdb 
conn = MySQLdb.connect (host = "localhost", 
         user = "testuser", 
         passwd = "testpass", 
         db = "test") 
cursor = conn.cursor() 
cursor.execute("SELECT * FROM thetable") 
while True: 
    row = cursor.fetchone() 
    if row is None: break 
    # here: do something with the row 

當然,你可以每行寫一個csv文件與Python的標準庫csv模塊 - 您只需在代碼開始時使用import csv。然後,cursor.execute後,您可以使用如下代碼:如果你想寫一個.xls文件而不是.csv

with open('thefile.csv', 'w') as f: 
    writer = csv.writer(f) 
    while True: 
     row = cursor.fetchone() 
     if row is None: break 
     writer.writerow(row) 

,請參閱第三方模塊xlwt

+0

非常感謝Mr.Alex Martelli ......... – Nimmy 2010-03-20 05:29:46

+0

xlwt刪除古董版本號的編輯網址 - 默認是最新版本,它可以節省支持團隊的磨損:-) – 2010-03-20 06:53:28

相關問題