If statement through an iteration, error: 'float' object cannot be interpreted as an integer

If statement through an iteration, error: 'float' object cannot be interpreted as an integer

您好,我正在尝试通过迭代 excel 个单元格来使用该函数。但是我得到错误 'float'(w_s) object cannot be interpreted as an integer。我正在尝试通过 'w_s' 中的值搜索 10-11 范围内的所有值。谁能建议我修改我的代码?

import openpyxl
import numpy

import os
os.system('cd c:\Code')

wb = openpyxl.load_workbook('SPLtest.xlsx')
turbine_data = wb.get_sheet_by_name('Turbinedata')
windspeed = wb.get_sheet_by_name('SPLvalues')


for row in range(2, windspeed.max_row+1):
    w_s = windspeed['A' + str(row)].value
for value in range(w_s):
    if value in numpy.arange(10.00,11.00,0.01) :
        print('107')
    else:
        print('no')

问题是,当您从 excel sheet 中提取数据时,它们变成浮点数。 range 函数只接受整数,因为使用非整数索引没有意义。就说

for value in range(int(w_s)):

你的脚本有很多问题,但你想要的一个改变是 np.linspace 而不是 np.arange,假设你想遍历看起来像 [10.00, 10.01, 10.02, ..., 10.98, 10.99, 11.00] 的东西。

所以你想做:

if value in numpy.linspace(start=10,stop=11,num=101) :
    print('107')
else:
    print('no')

np.linspace 将为您提供 numstartstop(含)之间的线性间隔值。