Python:strptime() 不工作

标签 python python-2.7

<分区>

我正在尝试将字符串转换为日期。如果我正在阅读 doc's正确的话,下面的代码应该可以工作:

import datetime
d = datetime.strptime('January 01, 2015', '%B %d, %Y')

相反,我得到了错误:

AttributeError: 'module' object has no attribute 'strptime'

最佳答案

您需要从 datetime 导入 datetime:

from datetime import datetime
d = datetime.strptime('January 01, 2015', '%B %d, %Y')

或者使用:

import datetime
datetime.datetime.strptime('January 01, 2015', '%B %d, %Y')

关于Python:strptime() 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30880194/

相关文章:

python - 将帖子标题转换为 CamelCase

python - 使用arrow python将人性化时间转换为UTC时间

html - 导出 csv 元素的 css 路径是什么

python - 是什么导致 Python 提示在没有列表的代码中列出列表?

python - dictproxy的目的是什么?

python - 如何使用 TensorFlow 正确实现动态卷积神经网络

python正则表达式查找外野手的名字

python - 如何将元组中的字符串转换为整数?

python - Sublime Text 中多行的正则表达式匹配

python - 计算每 5 行特定列的平均值,并选择 pandas 数据框中另一列的最后一个数据(第五个)