#!/usr/bin/env python
# coding: utf-8
import MySQLdb
import os,sys
import time
import datetime
from pyExcelerator import *
def main():
'''get datas from mysql to excel'''
w=Workbook()
ws=w.add_sheet('user')
mysql_conn=MySQLdb.connect(................,charset="utf8")
cursor=mysql_conn.cursor()
cursor.execute("select * from students")
results=cursor.fetchall()
results_count=len(results)
cursor.close()
mysql_conn.close()
a=results_count-1
print a
#print results
row=0
for r in results:
r3=[(x[0:2],x[2],x[3:]) for x in r]
w3=datetime.strptime("%Y-%m-%d %H:%M:%S")
[ws.write(x[0:2],i) for i in r3]
[ws.write(w3,i) for i in r3]
[ws.write(x[3:],i or '') for i in r3]:
row+=1
w.save('data.xls')
if __name__ == "__main__":
main()
我想从MySQL到Excel中获取数据,但 r3=[(x[0:2],x[2],x[3:]) for x in r]
给我TypeError:'datetime.datetime' object is not subscriptable
。类型错误: 'datetime.datetime' 对象不是标化
我不知道该怎么做,而我只学习了3周,请帮帮我吗?
写入单元格时,还需要设置'XFStyle()'样式对象; 'style = XFStyle(); style.num_format =“YYYY-MM-DD”或类似的。 –