python datetime.strptime 属性错误

1 投票
1 回答
1534 浏览
提问于 2025-04-17 14:14
import datetime

print datetime.strptime("00", "%y").year

我想把“00”传入datetime.strptime这个函数,然后得到“2000”作为输出。但是,当我运行代码时,总是出现这个错误:AttributeError: 'module' object has no attribute 'strptime'

1 个回答

3

strptime 这个东西在 datetime 模块的最上层是找不到的。它其实是 datetime 类型里的一个类方法,所以:

print datetime.datetime.strptime("00", "%y").year

撰写回答