Python 'datetime.datetime' 对象不可订阅

标签 python

首先,我不是 Python 开发人员。我正在尝试修复从命令行执行的 python 脚本中的问题。这个脚本是由不再存在并且不再愿意帮助解决问题的人编写的。

这是python 2.5,暂时不能升级。以下是有问题的代码行:

    start_time = datetime.strptime(str(paf.Start),"%Y-%m-%d %H:%M:%S")
    dict_start = datetime(*start_time[:6])

    end_time = datetime.strptime(str(paf.End),"%Y-%m-%d %H:%M:%S")
    dict_end = datetime(*end_time[:6])

运行此代码时,它会生成一个错误,描述为:'datetime.datetime' object is unsubscriptable。

这是导入语句:

from datetime import datetime

我觉得这很简单,但不是我的母语而且谷歌没有产生任何有值(value)的结果,我被卡住了。我尝试了几种不同的导入方法,产生了不同的错误,但都使用了这些语句。对此的任何帮助将不胜感激。

最佳答案

看起来你只是想要时间,对吗? datetime.strptime 方法返回一个“datetime”对象,因此以下属性包含时间:datetime.day、datetime.hour、datetime.year 等。

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

相关文章:

python - 如何阻止多个进程创建另一个进程的多个实例?

python - 在 python 中将原始数据类型名称作为函数参数传递

python - 将信号从多个工作线程发送到 GUI 线程

python - 感兴趣区域中的 OpenCV 模糊

python - jupyter notebook 显示 matplotlib Bad key "text.kerning_factor"的错误消息

python - 共享可变的问题?

python xlsxwriter根据行数据写入单元格

python - 值错误 : Wrong number of items passed - Meaning and suggestions?

python - cx_Oracle : how do I get the ORA-xxxxx error number?

python - 如何获取书签的页码