2017-07-31 4 views
-2

に対してstrない:コードは、Python 2.7で実行されていたが、エラーTypeError例外を与える:リストインデックスは、整数またはスライスでなければなりません、私が与えるとエラーを以下、このフェデックスバッチトラッキングコードを持っているバージョン3.5

runfile('***/Fedex Batch Track/trackitall.py', wdir='***/Fedex Batch Track') 
{'Ship Date': '7/22/2017', 'Actual Delivery': '7/28/2017'} 
Traceback (most recent call last): 

    File "<ipython-input-262-91e531a602be>", line 1, in <module> 
    runfile('***/Fedex Batch Track/trackitall.py', wdir='***/Fedex Batch Track') 

    File "***\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 880, in runfile 
    execfile(filename, namespace) 

    File "***\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile 
    exec(compile(f.read(), filename, 'exec'), namespace) 

    File "***/Fedex Batch Track/trackitall.py", line 55, in <module> 
    evaluate_excel("trackthis1","tracking_column","output3.txt") 

    File "***/Fedex Batch Track/trackitall.py", line 32, in evaluate_excel 
    for x in sheet.col_values(tracking_column): 

    File "C:\Users\Freight\Anaconda3\lib\site-packages\xlrd\sheet.py", line 531, in col_values 
    for rowx in range(start_rowx, end_rowx) 

    File "***\Anaconda3\lib\site-packages\xlrd\sheet.py", line 531, in <listcomp> 
    for rowx in range(start_rowx, end_rowx) 

TypeError: list indices must be integers or slices, not str 

私はできません理由を見つけてください。ここで

は完全なコード

from track import evaluate 
import xlrd 
import os 


def get_list(name, tracking_column): 
    dirname, filename = os.path.split(os.path.abspath(__file__)) 

    name_of_file = dirname + '/'+str(name) + '.xls' 

    book = xlrd.open_workbook(name_of_file) 
    sheet = book.sheet_by_index(0) 
    tracklist = [] 
    for x in sheet.col_values(tracking_column): 
     tracklist.append(x) 
     trackingnumber = x 

    return tracklist 


def evaluate_excel(name, tracking_column, outputfile): 

    dirname, filename = os.path.split(os.path.abspath(__file__)) 

    name_of_file = dirname + '/'+str(name) + '.xls' 
    outputfile = str(outputfile)+'.txt' 


    book = xlrd.open_workbook(name_of_file) 
    sheet = book.sheet_by_index(0) 
    tracklist = [] 
    for x in sheet.col_values(tracking_column): 
     tracklist.append(x) 
     trackingnumber = x 

    with open(outputfile, "wt") as text_file: 
     #os.chmod(name, 0600) 
     print('trackingnumber\tShipped\tArrived\tDelivered') 

     text_file.write('trackingnumber\tShipped\tArrived\tDelivered\t') 

     for x in tracklist: 
      y = x 
      a,b,c = evaluate(x) 
      if a != "": 
       text_file.write('\n'+str(y) + "\t" +str(a)+"\t"+str(b) + "\t" + str(c)+'\t') 
       print(str(y) + "\t" +str(a)+"\t"+str(b) + "\t" + str(c)+'\t') 

      else: 
       print(str(y) + "\t" + 'None') 
       text_file.write('\n'+str(y) + "\t" + 'None') 


#get_list("trackthis1","tracking_column") 
#evaluate_excel("trackthis1","tracking_column","output3.txt") 
+2

質問を書くとき、ページにプレビューが表示されます。あなたはそれを使うべきです。 –

+2

'tracking_column'はコードの外で定義されています。しかし、おそらく文字列を返す 'input'から来るでしょう... –

答えて

0

は、私は、次の

for x in tracklist: 
      y = x 
      a,b,c = evaluate(x) 
      if a != "": 
       text_file.write('\n'+str(y) + "\t" +str(a)+"\t"+str(b) + "\t" + str(c)+'\t') 
       print(str(y) + "\t" +str(a)+"\t"+str(b) + "\t" + str(c)+'\t') 

を変更しようと次のとおりです。

for x in tracklist: 
      y = int(x) 
      a,b,c = evaluate(x) 
      if a != "": 
       text_file.write('\n'+str(y) + "\t" +str(a)+"\t"+str(b) + "\t" + str(c)+'\t') 
       print(str(y) + "\t" +str(a)+"\t"+str(b) + "\t" + str(c)+'\t') 

ないこれが機能するかどうか確認してくださいかどうか、私は知っていますそれはpython 3はものについての痛みであることができます。あなたはまた、すべてのモジュールが3つのように動作するわけではないことを覚えておかなければなりません。それはあなたの問題かもしれないので、それはあなたの問題かもしれません。

関連する問題