0

Excel シートのデータを (同じ図に) プロットしようとしていますが、さまざまな材料に対応する文字列の可変長リストを入力として使用したいと考えています。次のエラーが表示されます: TypeError: 'NoneType' object is not iterable 理由がよくわかりません。コードは次のとおりです。

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
4

1 に答える 1

0

質問で説明した TypeError を再現できませんでした (transmittance()パラメーターなしで呼び出した場合、おそらく取得されるでしょう)。ただし、Google で見つけた同じ名前の XLS ファイルで関数を呼び出すと、代わりに 2 つの異なるエラーが発生します。

  • のアイテムを繰り返しますがglass、現在のリストではなくリスト全体と比較しますitem
  • リストを作成するとき、テーブルにいくつかの空の文字列があるため、new_transm_index単にキャストすることはできません。floatこの場合、値はゼロであると想定します。

最後に、 (コメントで説明されているように)new_transm_index各アイテムのリストを保持するglass場合は、辞書を使用して、アイテム(キー)をそれぞれのリスト(値)にマッピングする必要があります。

...
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()
...
于 2013-04-22T15:10:24.203 に答える