python读取excel时,数字自动转化为float

时间:2021-08-08 23:46:05

xlrd模块去读excel时会将数字类型的自动转化为浮点数,这是一个小坑。在网上查了一下,该模块的作者也说过Excel treats all numbers as floats. In general, it doesn't care whether your_number % 1 == 0.0 is true or not

我们可以简单的判断读取的是不是数字,然后将其转化为int

# ctype为2时表示为number
cell = table.cell(0, 0)
if cell.ctype == 2 and cell.value % 1 == 0:
cell_value = int(cell.value)