2017-09-15 53 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] 

任何有關我在哪裏調用超出循環範圍的索引的想法?我浪費了很多時間,並且非常感謝一些幫助。

編輯: 在添加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)。

+0

您應該刪除'try' /'except'並查看完整的錯誤回溯。你可以在'except'塊中添加一個簡單的'raise'來重新引發異常。 –

回答

1

經過兩天我對這個問題不知所措,我意識到我的原始輸入有「非空格」而不是「空格」。當我用MS-Word粘貼輸入並啓用格式化字符顯示時,我意識到了這一點。要解決它,我明確爲「UTF-8」通過添加下面的設置默認的Python編碼:

import sys 
# encoding=utf8 

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

和中提琴!它工作得很好。

感謝您幫助鄉親們。如果我提出這個問題的方式有些不完整,我很抱歉。

0

這很不幸是一種猜測(部分原因是因爲您在打印line而不是content時出現錯誤),但是在輸入中是否會有TAB字符而不是空格?發生故障的

split('] ')[1] 

意味着字符串'] '沒有出現在該線路(的一部分)。

更一般地說:試着重寫這個文件根本不打開任何文件(你從頭開始就已經擁有了所有的字符串)。簡化的數據流應該使調試更容易。