发布时间:2019-09-13 09:26:30编辑:auto阅读(2220)
每次给运营导数据的时候,如果不用工具的话,就是直接生成.csv格式的文件,这样的文件不支持'sheet',每次还有手工进行,相当的不科学,今天试试Python生成excel文件。
写的糙了点,但是这是一个很好的开始!
改进版本:数据库内容到EXCEL
#!/usr/bin/env python
import xlwt
import MySQLdb
wbk=xlwt.Workbook()
sheet=wbk.add_sheet('sheet 1')
sheet.write(0,0,'company')
sheet.write(0,1,'city')
row=1
conn=MySQLdb.connect(host='XXX',user='XXX',passwd='XXX',db='gh')
cursor=conn.cursor()
cursor.execute('select * from customers')
for com,city in cursor.fetchall():
sheet.write(row,0,com)
sheet.write(row,1,city)
row+=1
wbk.save(city.xls)
cursor.close()
conn.commit()
conn.close()
参考地址:http://scienceoss.com/write-excel-files-with-python-using-xlwt/
上一篇: Python [4] Django的安装
下一篇: Python第五周 学习笔记(2)
51964
51754
42050
38891
33386
30349
28993
24009
23924
22282
472°
2648°
3341°
2764°
2753°
3526°
2715°
3543°
5839°
5630°