发布时间:2019-09-13 09:26:30编辑:auto阅读(2092)
每次给运营导数据的时候,如果不用工具的话,就是直接生成.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)
51324
50779
41373
38176
32662
29552
28393
23270
23237
21562
1641°
2374°
1980°
1919°
2247°
1953°
2648°
4440°
4275°
3045°