2

可能重复:
excel文件中的整数变成浮点数?

我有一个包含 的 excel 电子表格,1984xlrd 将其作为一种number类型处理,因此将值返回为 float 1984.0。我想获取电子表格中显示的原始值,作为字符串"1984"。我怎么得到这个?

4

4 回答 4

2

So internally in Excel, that 1984 is stored as a decimal number, so 1984.0 is correct. You could have changed the number formatting to show it as 1984.00, or whatever.

So are you asking how to query the cell formatting to tell that the number format is no decimals? If so you might look into using the formatting_info=True parameter of open_workbook

sheet = open_workbook( 'types.xls',formatting_info=True ).sheet_by_index(0)

Have you come across the python-excel.pdf document from http://www.python-excel.org/ ? It is pretty good tutorial for learning to use xlrd and xlwt. Unfortunately, they say:

We've already seen that open_workbook has a parameter to load formatting information from Excel files. When this is done, all the formatting information is available, but the details of how it is presented are beyond the scope of this tutorial.

于 2012-11-09T22:27:09.887 回答
1

如果 cell.ctype==xlrd.XL_CELL_NUMBER

然后 excel 将 1984 存储为浮点数,您需要在 python 中转换为字符串

在excel中

="1984" 将是一个字符串 '1984 将是一个字符串,注意 ' 不显示 1984 是一个 #

于 2012-11-09T22:09:37.987 回答
0

唯一的数字是浮点数。附加到单元格的格式确定它是表示日期、小数还是整数。查找格式字符串,希望它能让您辨别数字的显示方式。

于 2012-11-09T22:07:33.077 回答
-1

使用字符串格式:

"%d" % mynumber
>>> "%d" % 1984.0
'1984'
于 2012-11-09T22:03:26.423 回答