简体   繁体   English

Python TypeError:“ datetime.datetime”对象不可下标

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

I've a query inside python script which connects to the sql database and retrieves a (datetime, Id) pair for that respective row. 我在python脚本中有一个查询,该查询连接到sql数据库并为相应的行检索一个(日期时间,Id)对。 I need to iterate through the result set and filter out the 'datetime' and 'Id' part separately. 我需要遍历结果集并分别过滤掉“ datetime”和“ Id”部分。 My intention is to get 'Id' for each row. 我的意图是为每一行获取“ Id”。 So in the following query I need to filter out "275" (see below) 因此,在以下查询中,我需要过滤掉“ 275”(如下所示)

On writing this script: 在编写此脚本时:

cursor2.execute(query2, [item[0]])
values = cursor2.fetchone() 
#values now equals = (datetime.datetime(2015, 7, 22, 17, 17, 36), 275)
print(values[0][1])

I get this error: 我收到此错误:

TypeError: 'datetime.datetime' object is not subscriptable TypeError:“ datetime.datetime”对象不可下标

I have tried converting values to a list/string objects but nothing has been working so far. 我试过将值转换为列表/字符串对象,但到目前为止没有任何工作。 Any ideas? 有任何想法吗?

If you are simply trying to get the complete datetime object, then simply use values[0] , instead of values[0][0] . 如果您只是想获取完整的datetime对象,则只需使用values[0] ,而不是values[0][0] And for Id use values[1] . 对于Id使用values[1] Example - 范例-

>>> values = (datetime.datetime(2015, 7, 22, 17, 17, 36), 275)
>>> print(values[1])
275

values[0] refers to the datetime object , so when you do values[0][1] , you are trying to use subscript on the datetime object, which is not possible, and hence the error. values[0]指向datetime对象,因此,当您执行values[0][1] ,您将尝试在datetime对象上使用下标,这是不可能的,因此会出现错误。

This is because you are using cursor.fetchone() , which only returns you a single row as a tuple. 这是因为您使用的是cursor.fetchone() ,它仅返回单行作为元组。 If you were instead using .fetchall() or .fetchmany() , then what you get would be a list of tuples and in that case as well, you can iterate over the list , taking one tuple at a time, and getting the element at index 1 . 如果您改用.fetchall().fetchmany() ,则得到的将是一个元组列表,在这种情况下,也可以遍历该列表,一次取一个元组,并获取元素在索引1 Example - 范例-

for dateobj, id in cursor.fetchall():
    #Do your logic with `id`.

when you call .fetchone() you get back a tuple (one record): 当您调用.fetchone()您将返回一个元组(一个记录):

mydate, myid = cursor.fetchone()

if you just want to get the id for each row you could do: 如果您只想获取每一行的id ,则可以执行以下操作:

ids = [record[1] for record in cursor.fetchall()]

generally, it's better to only select the data you need though, maybe: 通常,最好只选择所需的数据,也许:

cursor.execute("select id from ({subquery}) t".format(subquery=query2), [item[0]])   # assuming the id column is named id
ids = [record[0] for record in cursor.fetchall()]  # now we're only retrieving one column (index zero)

To get 275, you just need 要获得275,您只需要

print(values[1])

assuming 假设

values == (datetime.datetime(2015, 7, 22, 17, 17, 36), 275)

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

相关问题 TypeError:'datetime.datetime'对象不可下标 - TypeError:'datetime.datetime' object is not subscriptable 'datetime.datetime'对象不可下标 - 'datetime.datetime' object is not subscriptable 如何修复 Python TypeError: 'datetime.datetime' object is not callable? - How to fix Python TypeError: 'datetime.datetime' object is not callable? 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' Python'datetime.datetime'对象不可取消 - Python 'datetime.datetime' object is unsubscriptable 将 Python datetime.datetime 对象插入 MySQL - Inserting a Python datetime.datetime object into MySQL 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'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM