2013-04-18 78 views
0

我试图从Excel工作表中绘制出一些数据(在同一图上),并且我想将不同材质对应的可变长度字符串列表作为输入。我得到以下错误: TypeError:'NoneType'对象不可迭代 我真的不明白为什么。下面是代码:Python:具有可变长度参数列表的函数

import xlrd 
import matplotlib.pyplot as plt 
from numpy import * 
def transmittance(*glass): 
    wb=xlrd.open_workbook('schott_optical_glass_catalogue_excel_december_2012.xls') 
    sheet1=wb.sheet_by_index(0) 
    transm_index=[] #lista vuota 
    plt.figure(1) 
    plt.xlabel('wavelength $\lambda$[nm]') 
    plt.ylabel('transmittance') 
    for item in glass: 
     for i in range(sheet1.nrows): 
      if sheet1.cell_value(i,0)==glass: 
       reversed_transmission=sheet1.row_values(i,37,67) 
       transm_index=reversed_transmission[::-1] 
       new_transm_index=[float(ni) for ni in transm_index ] 
    wavel_range=sheet1.row_values(3,37,67) 
    temp_wavel= [k.split('/')[1] for k in wavel_range] 
    wRange=map(int,temp_wavel[::-1]) 
    plt.plot(wRange,new_transm_index, 'r-') 
    plt.grid(True, which="both") 
    plt.show() 
    return new_transm_index, wRange 

if __name__=='__main__': 
    new_transm_index=transmittance('N-BASF64','N-BK7') 
    print 'get tuple length and glass name: ' ,new_transm_index 
+0

如果您为该语言添加标签,则可能会得到更多答复。 –

+0

我编辑了空格,使它至少在语法上是正确的,但我不确定这正是你想要的,@ Ivranovi - 你可以检查它吗? –

+1

另外,你能告诉我们你如何称之为“透光度”,错误的全文是什么? –

回答

0

我无法重现你的问题你所描述的类型错误(我可能会得到,如果我叫transmittance()不带任何参数)。但是,当我用通过Google找到的具有相同名称的XLS文件调用函数时,我得到两个不同的错误。

  • 你迭代的项目中glass,但再比较整个列表,而不是当前的item
  • 当您创建new_transm_index列表中,你不能只是投给float,因为有一些空表中的字符串;在这种情况下,我会假设该值为零。

最后,如果你想new_transm_indexglass持有列表每个项目(如您的评论描述的),你应该使用一本字典,映射项(键)到相应的列表(值)。

... 
new_transm_index = {} # create dictionary 
for item in glass: 
    for i in range(sheet1.nrows): 
     if sheet1.cell_value(i, 0) == item: # compare to item, not to list 
      ... 
      # do not cast ' ' to float, but use 0 if not of type float 
      new_transm_index[item] = [ni if type(ni) == float else 0 for ni in transm_index] 
... 
for item in glass: # add plots for all items to common diagram 
    plt.plot(wRange,new_transm_index[item]) 
plt.grid(True, which="both") 
plt.show() 
... 
+0

cool!非常感谢!有没有办法用不同的颜色自动绘制曲线? – Ivranovi

+0

@Ivranovi当我测试它时,它_did_以不同的颜色自动绘制曲线;一个用蓝色,另一个用绿色。这不适合你吗? –

+0

对不起,我不得不从plt.plot中删除'r-' – Ivranovi

相关问题