AttributeError:'time.struct_time'对象没有' toordinal '属性

5 投票
1 回答
15434 浏览
提问于 2025-04-17 08:31

我刚接触Python,对如何正确格式化日期有点困惑。

我的数据是这样的 Fri, 09 Dec 2011 06:50:37 UTC

我准备数据的方式是这样的:

dates.append(time.strptime(row[5], "%a, %d %b %Y %H:%M:%S %Z"))

然后我尝试使用它

dates = matplotlib.dates.date2num(dates)

结果出现了以下错误:

AttributeError: 'time.struct_time' object has no attribute 'toordinal'

1 个回答

5

你正在使用time模块,但matplotlib需要的是datetime对象。

可以试试下面这样的写法:

from datetime import datetime

dates.append(datetime.strptime(row[5], "%a, %d %b %Y %H:%M:%S %Z"))
...

撰写回答