我正在使用 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
甚至不是一根字符串!
@sitWolf:
检查您的索引,尝试用 [i][1] 代替 [1][i] 表示湿度,[i][0] 表示摄氏度,[i][0] 表示华氏度
我的数据集包含那些翻转的索引。
@slothrop:
可以使用像
这样的循环来写得更干净(并且更不容易出错),这样您就可以像for hist in histTuple
、hist[0]
和hist[1]
那样访问每个元组内的值。hist[2]
我不知道为什么我没有想到这一点,谢谢!