2017-03-05 69 views
0

我已经写了一个代码来从文件中读取数据进行插值,但是当我运行该程序时说的是在列表中出现索引错误“pointsx.append(float(words [0])) :从您的评论python'列出索引错误'

with open("points.dat","r") as f: 
    data = f.readlines() 
    pointsx = [] 
    pointsy = [] 
    for line in data: 
      words=(line.split()) 
      pointsx.append(float(words[0])) 
      pointsy.append(float(words[1])) 

x = float(input('Enter the value of X:\n')) 

i=0 
lenx=len(pointsx) 

if x<pointsx[0]:\n 
     print("this Particular value of X is lower than the range of interpolation \n") 
elif x>pointsx[lenx-1]: 
     print("This particular value of x is higher than the range of interpolation\n") 
else: 
     for i in range(lenx-1): 
       if(x<=pointsx[i]): 
         break; 


       y=pointsy[i-1]+((pointsy[i]-pointsy[i-1])/(pointsx[i]-pointsx[i-1]))*(x-pointsx[i-1]) 
       print("The required value of y is ",y,"\n") 


f.close() 
+0

在那里。任何你的文件包含em的机会pty行? (即使在最后) –

+0

哦,非常感谢你。最后有一个空行。现在已经解决了。 :) –

回答

0

结论,words=(line.split())填充words[]因此,pointsx.append(float(words[0]))试图达到的words会抛出异常的第一个元素 该行前检查的words大小