openpyxl:从Excel中读取浮点数精度的损失?

我正在使用OS X,并在Excel工作簿中添加一些数据,这些数据被格式化为数字(这里是Excel中数据的Dropbox链接 )

Excel中的数据

现在,我正在尝试使用openpyxl在Python中导入它们:

from openpyxl import load_workbook # opening the file .xlsx file_dir = './data' file_xlsx = file_dir + '/db_StocksHighFreq.xlsx' # with openpyxl wb1 = load_workbook(file_xlsx) IBM_wb1 = wb1.sheet_by_name('IBM') # "Time" time series Time = wb1['IBM'].columns[0][1:] # "Price" time series Price = wb1['IBM'].columns[1][1:] 

OUTPUT:

1)“时间”正确读取为datetime.time对象

 In [23]: Time[0].value Out[23]: datetime.time(9, 30) In [24]: type(Time[0].value) Out[24]: datetime.time 

2),但“价格”时间序列,读为浮动,似乎被截断…

 In [25]: Price[0].value Out[25]: 205.85 In [26]: type(Price[0].value) Out[26]: float In [27]: Price[17].value Out[27]: 206.18 In [28]: Price[17].value < 206.18 Out[28]: False 

而不是206.1799,这也是如何在Excel中显示的(单元格B19)。

任何解决scheme 感谢您的关注。

任何不使用pandas理由?

 import pandas as pd df = pd.read_excel('db_StocksHighFreq.xlsx') print df.head() >> Time Price 20 Sep 2012 Price 21 Sep 2012 0 09:30:00 205.8500 207.3600 1 09:31:00 205.3900 207.0600 2 09:32:00 206.3600 207.0000 3 09:33:00 206.0100 207.2000 4 09:34:00 206.5000 207.4700 5 09:35:00 206.5650 207.2400 6 09:36:00 206.6100 207.3400 7 09:37:00 206.8500 207.3900 8 09:38:00 206.9200 207.4300 9 09:39:00 206.9100 207.4000 10 09:40:00 206.9500 207.4200 11 09:41:00 206.6500 207.2800 12 09:42:00 206.4400 207.1000 13 09:43:00 206.3300 207.0600 14 09:44:00 206.3100 206.9600 15 09:45:00 206.2300 206.9200 16 09:46:00 206.3000 207.1300 17 09:47:00 206.1799 206.9700 18 09:48:00 206.0240 206.9000 19 09:49:00 206.0900 206.9900 . . . print df['Price 20 Sep 2012'].dtype >> float64 

加布里埃莱,

语法sheet_by_name没有为我工作,所以我相信它必须在2.3.1中过时。 您的代码在我使用时工作:

 IBM_wb1 = wb1.get_sheet_by_name('IBM') 

您可以使用更新您的openpyxl版本

 pip install openpyxl --upgrade 

通过您的其他代码,我没有遇到任何问题,并能够输出206.1799。