9
#!/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()

I want get data from mysql to excel ,but r3=[(x[0:2],x[2],x[3:]) for x in r] gives me TypeError:'datetime.datetime' object is not subscriptable.

I do not know how to about it, and I just study only 3 week, please help me?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
user1477974
  • 91
  • 1
  • 1
  • 2

3 Answers3

7

x is a datetime.datetime object which cannot be use with the [] notation as in x[0:2].

It means that one of your columns holds a date object which must be parsed differently.

Simon Bergot
  • 10,378
  • 7
  • 39
  • 55
2

Firstly, you don't want to be using pyExcelerator - it's old and hasn't been updated in 3 odd years (and has been superseded).

What you should be using is the utilities at http://www.python-excel.org/ and this provides functions for working with datetimes. Excel stores these as floats since a certain epoch. For info https://secure.simplistix.co.uk/svn/xlrd/trunk/xlrd/doc/xlrd.html - under the section "Dates in Excel spreadsheets". Also see https://secure.simplistix.co.uk/svn/xlrd/trunk/xlrd/doc/xlrd.html#xldate.xldate_as_tuple-function for how to convert an excel representation of a date to a standard python datetime.

If you easy_install/pip xlutils, you'll get both the xlrd (reading) and xlwt (writing) libraries. Up to version 2003 files are supported, but 2007+ (.xlsx file) support is close to coming out of beta.

edit

Forgot to mention that https://secure.simplistix.co.uk/svn/xlwt/trunk/xlwt/doc/xlwt.html describes how the xlwt library can take a datetime.dateime and convert that to an Excel cell.

Jon Clements
  • 138,671
  • 33
  • 247
  • 280
  • You also need to set a `XFStyle()` style object when writing the cell; `style = XFStyle(); style.num_format = "YYYY-MM-DD"` or similar. – Martijn Pieters Jun 24 '12 at 12:42
0

One of the fields in your table seems to contain datetime objects, MySQLdb also returns them as datetime. You probably want to convert datetime to str first. That line seems to take some part of the datetime by using slices. You could achieve the same with datetime.strftime.

Lev Levitsky
  • 63,701
  • 20
  • 147
  • 175