简体   繁体   English

TypeError:'datetime.datetime'对象不可下标

[英]TypeError:'datetime.datetime' object is not subscriptable

#!/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 . 我想从mysql到excel获取数据,但r3=[(x[0:2],x[2],x[3:]) for x in r]给我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? 我不知道该怎么办,我只学习了3个星期,请帮助我吗?

x is a datetime.datetime object which cannot be use with the [] notation as in x[0:2] . xdatetime.datetime对象,不能与x[0:2]的[]表示法一起使用。

It means that one of your columns holds a date object which must be parsed differently. 这意味着您的一列中包含一个日期对象,该对象必须以不同的方式进行解析。

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). 首先,您不想使用pyExcelerator-它很旧,并且在3年的奇数年内没有进行过更新(已被取代)。

What you should be using is the utilities at http://www.python-excel.org/ and this provides functions for working with datetimes. 您应该使用的是http://www.python-excel.org/上的实用程序,它提供了用于处理日期时间的功能。 Excel stores these as floats since a certain epoch. 自某个时期以来,Excel将它们存储为浮点数。 For info https://secure.simplistix.co.uk/svn/xlrd/trunk/xlrd/doc/xlrd.html - under the section "Dates in Excel spreadsheets". 有关信息, 参见“ Excel电子表格中的日期”部分下的https://secure.simplistix.co.uk/svn/xlrd/trunk/xlrd/doc/xlrd.html 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. 另请参阅https://secure.simplistix.co.uk/svn/xlrd/trunk/xlrd/doc/xlrd.html#xldate.xldate_as_tuple-function,以了解如何将日期的Excel表示形式转换为标准python日期时间。

If you easy_install/pip xlutils, you'll get both the xlrd (reading) and xlwt (writing) libraries. 如果您easy_install / pip xlutils,则将同时获得xlrd(读取)和xlwt(写入)库。 Up to version 2003 files are supported, but 2007+ (.xlsx file) support is close to coming out of beta. 最多支持2003版文件,但2007 +(。xlsx文件)支持即将退出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. 忘记提及https://secure.simplistix.co.uk/svn/xlwt/trunk/xlwt/doc/xlwt.html描述了xlwt库如何获取datetime.dateime并将其转换为Excel单元格。

One of the fields in your table seems to contain datetime objects, MySQLdb also returns them as datetime . 表中的字段之一似乎包含datetime对象, MySQLdb还将它们作为datetime返回。 You probably want to convert datetime to str first. 您可能想先将datetime转换为str That line seems to take some part of the datetime by using slices. 通过使用分片,该行似乎占用了datetime的一部分。 You could achieve the same with datetime.strftime . 您可以使用datetime.strftime实现相同的目的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python TypeError:“ datetime.datetime”对象不可下标 - Python TypeError: 'datetime.datetime' object is not subscriptable 'datetime.datetime'对象不可下标 - 'datetime.datetime' object is not subscriptable TypeError:'datetime.datetime'对象不可调用 - TypeError: 'datetime.datetime' object is not callable datetime.datetime对象的总和给出了错误TypeError:+:'datetime.datetime'和'datetime.datetime'的不支持的操作数类型 - sum of datetime.datetime object gave an error TypeError: unsupported operand type(s) for +: 'datetime.datetime' and 'datetime.datetime' datetime TypeError:'datetime.datetime'对象没有属性'__getitem__' - datetime TypeError: 'datetime.datetime' object has no attribute '__getitem__' TypeError - datetime.datetime 不可调用 - TypeError - datetime.datetime is not callable TypeError: 描述符 'date' 需要一个 'datetime.datetime' 对象但收到了一个 'int' - TypeError: descriptor 'date' requires a 'datetime.datetime' object but received a 'int' 如何解决 'datetime.datetime' object is not iterable' TypeError - how to solve 'datetime.datetime' object is not iterable' TypeError TypeError: 'datetime.datetime' 对象在我生成 LazyDatetime 时不可调用 - TypeError: 'datetime.datetime' object is not callable when I generate LazyDatetime 如何修复 Python TypeError: 'datetime.datetime' object is not callable? - How to fix Python TypeError: 'datetime.datetime' object is not callable?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM