1.環(huán)境介紹:
- python版本:3.6.4
- pymysql: 0.8.0
- xlwt: 1.3.0
pymysql安裝
pip install pymysql
xlwt安裝
pip install xlwt
2.說(shuō)明
- 獲取字段信息
fields = cursor.description
- 獲取數(shù)據(jù)
數(shù)據(jù)項(xiàng)是一個(gè)類似于二維數(shù)組的存在买优。我們獲取每一個(gè)cell項(xiàng)的時(shí)候應(yīng)該注意援制。
results = cursor.fetchall()
- 創(chuàng)建workbook對(duì)象
workbook = xlwt.Workbook()
- 獲取sheet
``sheet = workbook.add_sheet('表名稱',cell_overwrite_ok=True)
3.代碼示例
注意:dominotypedata為數(shù)據(jù)庫(kù)名灌侣;mobile_login為表名字
import pymysql
import xlwt
conn = pymysql.connect(host='localhost',port=3306,user='root',passwd='password',db='dominotypedata',charset='utf8')
cursor = conn.cursor()
count = cursor.execute('select * from mobile_login')
# 打印出總共條數(shù)
print(count)
# 重置游標(biāo)位置,指定游標(biāo)位置從最初開(kāi)始
cursor.scroll(0,mode='absolute')
# 查詢所有結(jié)果
results = cursor.fetchall()
# print(results) 遍歷所有數(shù)據(jù)新症,在一個(gè)元組中
# for result in results:
# print(result) 一條條遍歷所有數(shù)據(jù)
# print('id:%s' % result[0])
# print('時(shí)間:%s' % result[1])
# print('uid:%s' % result[2])
# print('姓名:%s' % result[3])
# print('所屬地市:%s' % result[4])
# 獲取mysql里面的數(shù)據(jù)字段名稱
fields = cursor.description
workbook = xlwt.Workbook()
sheet = workbook.add_sheet('mobile_login',cell_overwrite_ok=True)
# 字段信息
for field in range(0,len(fields)):
sheet.write(0,field,fields[field][0])
# print(fields[field][0]) 打印出mysql字段名稱
# 獲取并寫(xiě)入數(shù)據(jù)庫(kù)字段信息
row = 1
col = 0
for row in range(1,len(results)+1):
for col in range(0,len(fields)):
sheet.write(row,col,'%s' % results[row-1][col])
workbook.save('./yh.xls')
print('導(dǎo)出結(jié)束!總共導(dǎo)出:%d 條數(shù)據(jù)刹前!' % count)
conn.close()
4.代碼封裝成函數(shù)調(diào)用
為了便于使用两入,可以將上面代碼封裝起來(lái)裳仆,調(diào)用的時(shí)候只需要傳入相關(guān)參數(shù)即可
import pymysql
import xlwt
def export(host,user,passwd,dbname,table_name,outpath):
conn = pymysql.connect(host,user,passwd,dbname,charset='utf8')
cursor = conn.cursor()
count = cursor.execute('select * from %s' % table_name)
# 打印出總共條數(shù)
print(count)
# 重置游標(biāo)位置,指定游標(biāo)位置從最初開(kāi)始
cursor.scroll(0,mode='absolute')
# 查詢所有結(jié)果
results = cursor.fetchall()
# print(results) 遍歷所有數(shù)據(jù)溉箕,在一個(gè)元組中
# for result in results:
# print(result) 一條條遍歷所有數(shù)據(jù)
# print('id:%s' % result[0])
# print('時(shí)間:%s' % result[1])
# print('uid:%s' % result[2])
# print('姓名:%s' % result[3])
# print('所屬地市:%s' % result[4])
# 獲取mysql里面的數(shù)據(jù)字段名稱
fields = cursor.description
workbook = xlwt.Workbook()
sheet = workbook.add_sheet('%s' % table_name,cell_overwrite_ok=True)
# 字段信息
for field in range(0,len(fields)):
sheet.write(0,field,fields[field][0])
# print(fields[field][0]) 打印出mysql字段名稱
# 獲取并寫(xiě)入數(shù)據(jù)庫(kù)字段信息
row = 1
col = 0
for row in range(1,len(results)+1):
for col in range(0,len(fields)):
sheet.write(row,col,'%s' % results[row-1][col])
workbook.save(outpath)
print('導(dǎo)出結(jié)束晦墙!總共導(dǎo)出:%d 條數(shù)據(jù)!' % count)
conn.close()
if __name__ == "__main__":
export('loclhost','root','password','dominotypedata','mobile_login','yhxt.xls')
運(yùn)行結(jié)果和封裝前的是一樣的肴茄。