2017-06-23 84 views
0
import xlrd 

file_path = r"C:\Users\Allen\Desktop\数据曲线.xlsx" 
table = xlrd.open_workbook(file_path).sheet_by_index(2) 
data_current = table.col_values(4)[1:] 
data_time = table.col_values(2)[1:] 
time_start = [] 
for i in data_current: 
    index = data_current.index(i) 
    if float(i) < 0.1 and float(data_current[index+1]) > 1: 
     print('come on!') 
    else: 
     print('a') 

我输入:为什么我的if语句测试浮点值总是为false?

data_current = ['0.0', '0.0', '1.44'] 

这只是一个例子,但真实数据遵循相同的结构。

为什么我的输出始终为a,而从来没有come on!

回答

1

data_current.index(i)将返回第一比赛,所以对于'0.0'index总是将是0

使用enumerate() function提供一种跑步指数来代替:

for index, i in enumerate(data_current): 

要考虑到index + 1可以有效索引的范围之外

+0

非常感谢! – cwl