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

问题描述 投票:0回答:2

我是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 "/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脚本。

python
2个回答
1
投票

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

import sys
# encoding=utf8

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

而且中提琴!它工作得很好。

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


0
投票

不幸的是这是一个猜测(部分是因为你在出现错误时打印line而不是content),但输入中是否有TAB字符而不是空格?失败了

split('] ')[1]

意味着字符串'] '不会出现在该行的(该部分)中。

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

© www.soinside.com 2019 - 2024. All rights reserved.