excel文件中的整数变成浮点数? [英] Integers from excel files become floats?

查看:348
本文介绍了excel文件中的整数变成浮点数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 xlrd 从 excel 文件中读取数据.

对于存储在文件中的整数,例如 63xlrd 将其解释为 number 类型的 63.0>.

为什么 xlrd 不能将 63 识别为整数?

假设 sheet.row(1)[0].value 给我们 63.0.如何将其转换回 63.

解决方案

查看 documentation,似乎单元格没有任何整数类型,它们只是浮点数.因此,这就是即使你写了一个整数也会得到浮点数的原因.

要将浮点数转换为整数,只需使用 int:

<预><代码>>>>整数(63.0)63>>>int(sheet.row(1)[0].value)63

I use xlrd to read data from excel files.

For integers stored in the files, let's say 63, the xlrd interprets it as 63.0 of type number.

Why can't xlrd recognize 63 as an integer?

Assume sheet.row(1)[0].value gives us 63.0. How can I convert it back to 63.

解决方案

Looking at the different cell types in the documentation, it seems that there isn't any integer type for cells, they're just floats. Hence, that's the reason you're getting floats back even when you wrote an integer.

To convert a float to an integer just use int:

>>> int(63.0)
63
>>> int(sheet.row(1)[0].value)
63

这篇关于excel文件中的整数变成浮点数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆