2017-09-15 1 views
0

私はPythonの初心者です。私は関数を定義しているコードに取り組んでおり、ユーザーが与えた一連の行をトリミングし、それを呼び出している別のPythonスクリプトに渡します。コードは以下の通りである:リストインデックスが範囲外です - ユーザー入力を取り除くことができません

import re 
import sys 


def calltrimport(): 
     f=open("temp1.txt","w") 
     print "Enter the complete call trace.\nMake Sure there are no extra or unnecessary white spaces in the content.\nPress Enter twice to finish input" 
     file_lines = iter(raw_input, '') # Get lines as input until an empty line. 
     file_content = '\n'.join(file_lines) # Join the file lines to one string, seperated by new-line. 
     f.write(file_content) 
     f.close() 

     num_lines = 0 
     with open("temp1.txt", 'r') as f: 
       for line in f: 
         num_lines += 1 

     print("Number of lines recorded in the entered text: ") 
     print(num_lines) 

     with open("temp1.txt","r") as f1: 
       f2=open("temp2.txt","w") 
       for content in range(0,num_lines): 
         try: 
           content = f1.readline() 
           sp = content.split('+')[0] 
           sp1 = sp.split('] ')[1] 
           sp1 = sp1.strip() 
           f2=open("temp2.txt","a") 
           f2.write(sp1+'\n') 
           f2.close 
         except IndexError, e: 
           print e 
           print "line = ", line 
     with open('temp2.txt', 'r') as myfile: 
       data=myfile.read().replace('\n', '') 
     return data 

私も今、サンプル入力と期待出力に入れなければならない。

Enter the complete call trace. 
Make Sure there are no extra or unnecessary white spaces in the content. 
Press Enter twice to finish input 

[<ffffffffXXXXXXXX>] garbage1+0x268/0x360 [Nope] 
[<ffffffffXXXXXXXX>] garbage2+0x412/0x470 [Nope] 
[<ffffffffXXXXXXXX>] garbage3+0x761/0x9b0 [Nope] 
[<ffffffffXXXXXXXX>] garbage4+0xfb/0x520 [Nope] 
[<ffffffffXXXXXXXX>] garbage5+0x3c/0x3a0 [Nope] 
[<ffffffffXXXXXXXX>] garbage6+0x65b/0xe00 [Nope] 
[<ffffffffXXXXXXXX>] garbage7+0x139/0x580 [Nope] 
[<ffffffffXXXXXXXX>] garbage8+0x1c0/0x1c0 [Nope] 
[<ffffffffXXXXXXXX>] garbage9+0x20e/0x760 [Nope] 

ファイルで期待される出力が「temp2.​​txt」が想定され次のように見て:

garbage1 
garbage2 
garbage3 
garbage4 
garbage5 
garbage6 
garbage7 
garbage7 
garbage8 

を代わりに、私は次のような出力が得られます。

Number of lines recorded in the entered call trace: 
9 
list index out of range 
line = [<ffffffffXXXXXXXX>] ? garbage1_+0x20e/0x760 [Nope] 
list index out of range 
line = [<ffffffffXXXXXXXX>] ? garbage1_+0x20e/0x760 [Nope] 
list index out of range 
line = [<ffffffffXXXXXXXX>] ? garbage1_+0x20e/0x760 [Nope] 
list index out of range 
line = [<ffffffffXXXXXXXX>] ? garbage1_+0x20e/0x760 [Nope] 
list index out of range 
line = [<ffffffffXXXXXXXX>] ? garbage1_+0x20e/0x760 [Nope] 
list index out of range 
line = [<ffffffffXXXXXXXX>] ? garbage1_+0x20e/0x760 [Nope] 
list index out of range 
line = [<ffffffffXXXXXXXX>] ? garbage1_+0x20e/0x760 [Nope] 
list index out of range 
line = [<ffffffffXXXXXXXX>] ? garbage1_+0x20e/0x760 [Nope] 
list index out of range 
line = [<ffffffffXXXXXXXX>] ? garbage1_+0x20e/0x760 [Nope] 

私のループの境界を越えてインデックスをどこに呼び出すのかについての考えはありますか?私はこれに多くの時間を無駄にして本当にいくつかの助けに感謝します。

編集: クラウスの助言としてexceptブロックにraiseを追加すると、私は次のようなトレースバック見ることができる:

Number of lines recorded in the entered call trace: 
9 
list index out of range 
line = [<ffffffffXXXXXXXX>] ? some_garbage+0x20e/0x760 [Nope] 
Traceback (most recent call last): 
    File "dts_check_final.py", line 13, in <module> 
    mystr = ct_imp.calltrimport() 
    File "https://stackoverflow.com/users/siddharath/dts/Final/ct_imp.py", line 26, in calltrimport 
    sp1 = sp.split('] ')[1] 
IndexError: list index out of range 

注:「dts_check_final.pyは」(上でこれを呼び出すPythonスクリプトからですct_imp.py)。

+0

'try' /' except'を削除し、完全なエラートレースバックを確認してください。 'except'ブロックに単純な' raise'を追加して例外を再発生させることができます。 –

答えて

1

この問題を2日間解消した後、の生の入力に「通常のスペース」ではなく「非区切りスペース」があることがわかりました。私はMS-Wordで入力を貼り付けたときにこれを認識し、書式設定文字を有効にしました。それを回避するために、私は明示的に「UTF-8」に次の行を追加して、デフォルトのPythonのエンコーディングを設定します。

import sys 
# encoding=utf8 

reload(sys) 
sys.setdefaultencoding('utf8') 

そしてヴィオラを!それはうまく動作します。

ご協力いただきありがとうございます。私が質問を提示したやり方がやや不完全だったなら、私の謝罪。

0

これは残念ながら推測ですが(エラーが発生した場合はcontentの代わりにlineの代わりに印刷していますが)スペースの代わりに入力にタブ文字が含まれる可能性がありますか?失敗

split('] ')[1] 

は、文字列'] 'はライン(の一部)に表示されていないことを意味します。

もっと一般的には、ファイルを一切開かないように書き直してください(最初からメモリ内にすべての文字列があります)。単純化されたデータフローにより、デバッグが容易になります。

関連する問題