我是一名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]
任何有關我在哪裏調用超出循環範圍的索引的想法?我浪費了很多時間,並且非常感謝一些幫助。
編輯: 在添加raise
作爲建議由克勞斯的except
塊,我可以看到下面的回溯:
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'來重新引發異常。 –