发布时间:2019-09-13 09:26:30编辑:auto阅读(2580)
每次给运营导数据的时候,如果不用工具的话,就是直接生成.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)
54121
40876
35386
31036
25924
25661
24108
19640
15707
15183
1938°
1782°
1871°
1951°
1945°
2127°
2078°
1898°
1981°
1968°