gpt4 book ai didi

python - TypeError :'datetime.datetime' 对象不可订阅

转载 作者:行者123 更新时间:2023-11-28 22:02:28 28 4
gpt4 key购买 nike

#!/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

我不知道怎么办,我才学了3周,请帮帮我好吗?

最佳答案

x 是一个 datetime.datetime 对象,它不能与 x[0:2] 中的 [] 符号一起使用。

这意味着您的其中一列包含一个必须以不同方式解析的日期对象。

关于python - TypeError :'datetime.datetime' 对象不可订阅,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11177293/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com