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