类型错误:“int”对象对于日期时间不可调用

问题描述 投票:0回答:1

尝试对 dt_obj 对象应用小时操作。我期望从 dt_obj 中提取小时,它是一个字符串。但我猜 dt_obj 是使用 hour() 操作的错误类型。

请原谅我的语言,我很新,不太了解我的类型。我认为这是因为我试图对错误的类型执行 hour() 操作,所以我收到了 int object is not callable 错误。

那么我需要做什么才能从 dt_obj 中提取小时?

日期格式 = '%m/%d/%Y %H:%M' 打印(日期格式) 将日期时间导入为 dt

`for row in result_list:
    time = row[0]
    print(time)
    dt_obj = dt.datetime.strptime(time, date_format)
    print(type(dt_obj))
    t_hour = dt_obj.hour()
    print(t_hour)



%m/%d/%Y %H:%M
8/16/2016 9:55
<class 'datetime.datetime'>
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-70-003a1e9f2cc7> in <module>
      8     dt_obj = dt.datetime.strptime(time, date_format)
      9     print(type(dt_obj))
---> 10     t_hour = dt_obj.hour()
     11     print(t_hour)

TypeError: 'int' object is not callable

Should be able to apply hour to the dt_obj to extra the hour but I think it's the wrong type of         object to apply it to.`
python datetime extract dt hour
1个回答
0
投票

不用括号,只需使用

dt_obj.hour
。这是必要的,因为
hour
是一个属性,而不是一个函数。

© www.soinside.com 2019 - 2024. All rights reserved.