简体   繁体   English

如何在python中使用openpyxl从Excel获取浮点值?

[英]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.

相关问题 Python:如何使用 openpyxl 为特定的 excel 表添加值? - Python: How to add value to a specific excel sheet using openpyxl? 如何使用 python openpyxl 从 excel 中按列名删除列 - how to delete columns by column name from an excel using python openpyxl 如何使用 openpyxl 从 excel 文件“按原样”获取数据? - How to get data from excel file 'as is' using openpyxl? 使用 python (openpyxl) 从 excel 中删除网格线 - Removing gridlines from excel using python (openpyxl) 如何使用openpyxl在python中按单元格值获取列号 - how to get column number by cell value in python using openpyxl 如何使用openpyxl python在excel中插入复选框? - How to insert checkbox in excel using openpyxl python ? 有什么方法可以使用openpyxl python从excel文件中读取单元格内每个单词的样式 - Is there any way to while reading from excel file get style of each word inside cell using openpyxl python 如何使用 openpyxl 使用 python 获取与 excel 中另一行对应的行数 - How to get row count corresponding to another row in excel using python using openpyxl 如何从 ZA7F5F35426B627411FCA430Z 中的 Windows 异步打开 Excel 工作簿中的 Excel 与 openpy1 - How to asynchronously open an Excel Workbook in Excel in Windows from Python with openpyxl 无法使用 OPENpyxl 获取某个 Excel 文件的值 - Can't get the value of a certain Excel file using OPENpyxl
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM