[英]How to get float value from excel using openpyxl in python?
I'm reading an Excel sheet and printing the data returned from it. 我正在读取Excel工作表并打印从中返回的数据。 But when the data in the cells are float values, the output is printed as Long int.
但是,当单元格中的数据为浮点值时,输出将打印为Long int。 How do I get the float value as is from the Excel sheet using openpyxl (using openpyxl because it has good documentation).
如何使用openpyxl从Excel工作表中直接获取浮点值(使用openpyxl,因为它具有良好的文档说明)。 I know I could convert using float keyword but this doesn't work if used inside a loop which gives string as output as well.
我知道我可以使用float关键字进行转换,但是如果在将字符串也作为输出的循环中使用,则无法使用。
Here is the sample excel data 这是示例excel数据
A B C D
1 Device Manufacturer LMP Version No.of Devices
2 DUT CSR 4.0 1
3 REF1 Broadcom 4.0 3
code is: 代码是:
import openpyxl
wb=openpyxl.load_workbook('/home/workspace/python/Book1.xlsx')
s = wb.get_sheet_by_name('Setup')
for j in s['A1':'D3']:
for cel in j:
print cel.coordinate,cel.value
print '-------------'
output is: 输出为:
A1 Device
B1 Manufacturer
C1 LMP Version
D1 No. of Devices
-------------
A2 DUT
B2 CSR
C2 4
D2 1
-------------
A3 REF1
B3 Broadcom
C3 4
D3 3
The LMP version is Long int, how do I get the float value? LMP版本是Long int,如何获取浮点值?
If there is a number stored as 4.0
actually excel stores it as 4
only and doesn't store the .0
in its internal structure. 如果存在一个存储为
4.0
的数字,则excel实际上仅将其存储为4
,并且不在其内部结构中存储.0
。 Hence in excel you need convert them in to text, rather than numbers. 因此,在excel中,您需要将它们转换为文本,而不是数字。
Consider this example excel file: 考虑以下示例excel文件:
Now unzip it using 7.zip or any other tool and navigate to sheet1.xml
like this: 现在,使用7.zip或任何其他工具将其解压缩,并导航至
sheet1.xml
如下所示:
Open the xml in any text editor (recommended notepad++) 在任何文本编辑器中打开xml(建议使用notepad ++)
and you will find that the .0
are not stored in it. 您会发现其中没有存储
.0
。
<cr="B4" s="2"><v>4</v></c>
<cr="B5" s="2"><v>5</v></c>
<cr="C5"><v>0.82548104138781497</v></c>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.