我试图从excel单元格中获取数据(数字格式)并将它们转换为Django 1.7.7中的日期类型:
import datetime as dt
birthdate =dt.date.fromtimestamp(sheet.row_values(idx)[63]).strftime('%Y-%m-%d')
但得到了这个错误:
File "t3_import2.py", line 46, in <module>
birthdate =dt.date.fromtimestamp(sheet.row_values(idx)[63]).strftime('%Y-%m-%d')
TypeError: a float is required
当我明确地将它转换为浮动时,为什么会抱怨?
答案 0 :(得分:0)
sheet.row_values(idx)[63]
不是浮动。
如果我理解正确,fromtimestamp
需要一个POSIX时间戳,它是一个向量,表示从给定时间到它所代表的时间/日期所经过的秒数。我无法看到你帖子中的sheet.row_values(idx)[63]
是什么,但我敢打赌它将它视为int
。
classmethod date.fromtimestamp(timestamp)¶
返回本地日期 对应于POSIX时间戳,例如返回的 了time.time()。如果时间戳不在,则可能会引发ValueError 平台C localtime()函数支持的值范围。它的 这种情况常见于1970年至2038年间。注意 在非POSIX系统上,包括闰秒的概念 时间戳,fromtimestamp()忽略闰秒。
试试这个:
x = float(sheet.row_values(idx)[63])
birthdate = dt.date.fromtimestamp(x).strftime('%Y-%m-%d')