列表索引超出范围 - 无法删除用户输入

时间:2017-09-15 08:49:49

标签: python

我是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]

知道我在哪里调用超出循环范围的索引?我浪费了很多时间,非常感谢你的帮助。

编辑: 在按照Klaus的建议将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 "/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”来自在(ct_imp.py)上调用它的python脚本。

2 个答案:

答案 0 :(得分:1)

经过两天对这个问题失去理智之后,我意识到我的原始输入有&#34;非破坏空间&#34;而不是&#34;常规空间&#34; 。当我在MS-Word中粘贴输入并启用格式化字符显示时,我意识到了这一点。为了解决这个问题,我明确地将默认的Python编码设置为&#34; UTF-8&#34;添加以下内容:

import sys
# encoding=utf8

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

和Viola!它运作得很好。

谢谢你帮助大家。如果我提出这个问题的方式有些不完整,我很抱歉。

答案 1 :(得分:0)

不幸的是,这是一个猜测(部分原因是,当您出现错误时,您正在打印line而不是content),但输入中可能有TAB字符,而不是空间?失败

split('] ')[1]

表示字符串'] '不会出现在该行的(该部分)中。

更一般地说:尝试将其重写为根本不打开任何文件(从一开始就已经拥有了内存中的所有字符串)。简化的数据流应该使调试更容易。