类型错误:类型 datetime.datetime 未定义 __round__ 方法

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

我正在使用 Python 开发 API。当尝试解析从数据库中获取的数据时,我收到此错误:

TypeError: type datetime.datetime doesn't define __round__ method

api.py:

response = []
for i in range(len(histTuple)):
        currentTime = histTuple[i][2].replace(microsecond=0)
        currentTime = str(currentTime) + f" GMT{TIMEZONE}"
        response.append({"humidity": round(histTuple[1][i], 1), "celsius": round(histTuple[0][i]), "fahrenheit": round(histTuple[0][i] * 9/5 + 32), "timestamp": currentTime}) # error here

我不想对

datetime.datetime
进行舍入,而且
currentTime
甚至不是一根字符串!

python rounding python-datetime
1个回答
0
投票

@sitWolf:

检查您的索引,尝试用 [i][1] 代替 [1][i] 表示湿度,[i][0] 表示摄氏度,[i][0] 表示华氏度

我的数据集包含那些翻转的索引。

@slothrop:

可以使用像

for hist in histTuple
这样的循环来写得更干净(并且更不容易出错),这样您就可以像
hist[0]
hist[1]
hist[2]
那样访问每个元组内的值。

我不知道为什么我没有想到这一点,谢谢!

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