天天看點

python實作excel篩選功能并輸出_Python怎樣實作資料篩選之後不存成excel?

展開全部

1 讀取Excel使用636f70793231313335323631343130323136353331333431336231xlrd#1、導入擴充包

import xlrd

#2、打開Excel檔案讀取資料

data = xlrd.open_workbook('excelFile.xls')

2 操作mysql使用MySQLdbimport MySQLdb

# 打開資料庫連接配接

db = MySQLdb.connect("localhost", "testuser", "test123", "TESTDB", charset='utf8' )

# 使用cursor()方法擷取操作遊标

cursor = db.cursor()

# 使用execute方法執行SQL語句

cursor.execute("SELECT VERSION()")

# 使用 fetchone() 方法擷取一條資料

data = cursor.fetchone()

print "Database version : %s " % data

# 關閉資料庫連接配接d

b.close()

3 寫入Excel使用xlwtimport xlwt

# 建立一個workbook 設定編碼

workbook = xlwt.Workbook(encoding = 'utf-8')

# 建立一個worksheet

worksheet = workbook.add_sheet('My Worksheet')

# 寫入excel

# 參數對應 行, 列, 值

worksheet.write(1,0, label = 'this is test')

# 儲存

workbook.save('Excel_test.xls')